fix(macros): accept const paths and concat! in tool/prompt descriptions - #1243
Open
atirna wants to merge 1 commit into
Open
fix(macros): accept const paths and concat! in tool/prompt descriptions#1243atirna wants to merge 1 commit into
atirna wants to merge 1 commit into
Conversation
The description field of #[tool] and #[prompt] only accepted a bare string literal, while #[doc] on the same item accepts include_str! and #[schemars] on the argument struct accepts const paths. Parse the field as an expression through darling's PreservedStrExpr, which keeps a string literal a literal, so a const path or concat! now works too. Fixes modelcontextprotocol#1175
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1175
Motivation and Context
descriptionin#[tool]and#[prompt]only accepted a bare string literal, while neighbouring attributes on the very same items are more permissive:#[doc]on the function acceptsinclude_str!and that value reaches the published description, and#[schemars(extend(...))]on the argument struct accepts const paths.So the published text could already be sourced from outside the attribute, just not through the field that names it. On a server with many tools, keeping the short broadcast description next to the longer fetchable one is much easier when both can be declared together and the short one referenced from the attribute:
How Has This Been Tested?
main: a small crate with#[tool(description = SOME_CONST)]fails witherror: Unexpected typepath, and `#[tool(description = concat!("a", "b"))]` fails with `error: Unexpected type `macro<fn>_tool_attr().descriptionequals the const value /"ab"; a bare string literal,#[doc]-derived andinclude_str!descriptions keep working;description = 42is still rejected with a normal type error at the attributetest_tool_macros.rs(const path,concat!) andtest_prompt_macros.rs(const path);cargo test -p rmcp-macros, the focused integration tests, and the fullcargo test -p rmcp --features "client server transport-io which-command"suite pass locally, pluscargo clippy --all-targets --all-features -- -D warningsandcargo +nightly fmt --all -- --checkImplementation notes: the field is parsed through darling's
PreservedStrExpr, whoseFromMetakeeps a string literal a literal instead of parsing its contents as code, so existingdescription = "..."usage is unchanged; the generated code stores a&'static streither way, exactly as before.Breaking Changes
None. Existing string-literal descriptions compile to the same code; only inputs that previously failed to compile are now accepted.
Types of changes
Checklist