-
Notifications
You must be signed in to change notification settings - Fork 578
feat: autocompletion for prompt args and resource template URI args #428
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
WalkthroughThis change introduces structured support for autocompletion of prompt and resource template arguments in the MCP server. It adds completion handler functions, new server capabilities, completion-specific hooks, and request handling for the new "completion/complete" method. Type safety and contextual information for completions are also improved. Changes
Assessment against linked issues
Assessment against linked issues: Out-of-scope changesNo out-of-scope changes were identified. All changes align with the objectives of adding context-aware completion support and related features. Suggested reviewers
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 golangci-lint (1.64.8)Error: you are using a configuration file for golangci-lint v2 with golangci-lint v1: please use golangci-lint v2 📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (1)
🔇 Additional comments (5)
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Nitpick comments (1)
server/server.go (1)
400-408
: Consider extracting completion capability check to reduce duplication.The logic for checking completion handlers in prompts is duplicated between
AddPrompts
andAddPrompt
. Consider extracting this to a helper method.Extract the completion handler check logic:
+func (s *MCPServer) checkPromptCompletionHandlers(prompts ...mcp.Prompt) { + for _, prompt := range prompts { + for _, arg := range prompt.Arguments { + if arg.CompletionHandler != nil { + s.implicitlyRegisterCompletionCapabilities() + return + } + } + } +} func (s *MCPServer) AddPrompts(prompts ...ServerPrompt) { s.implicitlyRegisterPromptCapabilities() s.promptsMu.Lock() for _, entry := range prompts { s.prompts[entry.Prompt.Name] = entry.Prompt s.promptHandlers[entry.Prompt.Name] = entry.Handler } s.promptsMu.Unlock() - for _, entry := range prompts { - for _, arg := range entry.Prompt.Arguments { - if arg.CompletionHandler != nil { - s.implicitlyRegisterCompletionCapabilities() - break - } - } - } + promptList := make([]mcp.Prompt, len(prompts)) + for i, entry := range prompts { + promptList[i] = entry.Prompt + } + s.checkPromptCompletionHandlers(promptList...) // When the list of available prompts changes, servers that declared the listChanged capability SHOULD send a notification. if s.capabilities.prompts.listChanged { // Send notification to all initialized sessions s.SendNotificationToAllClients(mcp.MethodNotificationPromptsListChanged, nil) } } func (s *MCPServer) AddPrompt(prompt mcp.Prompt, handler PromptHandlerFunc) { s.AddPrompts(ServerPrompt{Prompt: prompt, Handler: handler}) - for _, arg := range prompt.Arguments { - if arg.CompletionHandler != nil { - s.implicitlyRegisterCompletionCapabilities() - break - } - } + s.checkPromptCompletionHandlers(prompt) }Also applies to: 419-425
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
server/internal/gen/data.go
is excluded by!**/gen/**
📒 Files selected for processing (7)
mcp/prompts.go
(2 hunks)mcp/resources.go
(1 hunks)mcp/types.go
(5 hunks)mcp/utils.go
(1 hunks)server/hooks.go
(3 hunks)server/request_handler.go
(1 hunks)server/server.go
(6 hunks)
🔇 Additional comments (16)
mcp/resources.go (1)
101-110
: LGTM! Well-implemented functional option following established patterns.The
WithTemplateArgumentCompletion
function correctly follows the functional options pattern used throughout this file. The initialization of the map when nil and the assignment are properly handled.mcp/prompts.go (2)
71-72
: Good addition of optional completion handler field.The field is correctly defined as a pointer, making it optional and following Go conventions for optional struct fields.
174-179
: Excellent functional option implementation.The
ArgumentCompletion
function correctly follows the established functional options pattern for argument configuration.mcp/utils.go (1)
517-544
: Robust parsing function with comprehensive validation.The function correctly:
- Validates the structure of the completion reference
- Handles both resource and prompt reference types appropriately
- Provides clear error messages for validation failures
- Returns the correct reference type based on the parsed data
server/request_handler.go (1)
313-337
: Perfect implementation following established patterns.The completion method handler correctly follows the exact same pattern as other method handlers in this switch statement, including:
- Capability checking with appropriate error response
- Request unmarshaling with error handling
- Hook invocations (before/after/onError)
- Proper JSON-RPC response creation
server/hooks.go (3)
94-95
: Properly defined hook function types.The function signatures correctly match the pattern used for other hook types in this file.
124-125
: Correctly added fields to Hooks struct.The new fields follow the established naming convention and type pattern used for other hook collections.
538-564
: Excellent hook implementation following established patterns.The implementation correctly:
- Provides Add methods that append to the hook slices
- Implements before/after methods that call generic hooks (beforeAny/onSuccess)
- Iterates through all registered hooks appropriately
- Uses the correct method constant (mcp.MethodCompletion)
- Follows the exact same pattern as all other hook implementations
mcp/types.go (6)
54-57
: LGTM!The new method constant follows the established naming pattern and includes proper documentation.
77-79
: Function signature looks good.The handler function type is well-designed with proper context support and error handling.
83-85
: Well-structured field addition.The map-based approach for argument completion handlers is efficient and the JSON tag ensures backward compatibility.
979-983
: Context tracking implementation is clean.The addition of the Context field enables intelligent autocompletion based on previously completed arguments, which aligns well with the PR objectives.
1002-1003
: Type safety improvement.Converting to a strongly typed RefType improves type safety and prevents invalid reference types.
Also applies to: 1009-1010
1014-1020
: Well-defined reference type constants.The RefType definition with prefixed constants provides clear type discrimination for completion references.
server/server.go (2)
174-178
: Capability field follows established pattern.The completions capability field is correctly added as a boolean pointer, consistent with other capabilities.
469-475
: Capability registration follows established pattern.The method correctly implements the implicit registration pattern used by other capabilities.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Nitpick comments (4)
www/docs/pages/servers/prompts.mdx (4)
169-177
: Add descriptive preamble for Basic Argument Completion.
Without a brief explanation, readers might not immediately grasp what this example illustrates. Consider adding one or two sentences under the heading to explain that this demonstrates how to register prompt arguments with autocomplete handlers and leverage theContext
field for stateful suggestions.
181-184
: Clarify dictionary naming and structure.
Thedictionary
variable is generic—consider renaming tocompletionOptions
orjokeArgDictionary
for clarity. Also document its purpose (e.g., “maps argument names to possible suggestion values”).
186-194
: Use consistent prompt argument API.
This snippet usesmcp.WithArgument
andmcp.RequiredArgument()
, but earlier examples rely onmcp.WithPromptArgument
andmcp.Required()
. Align with the established naming to avoid confusion, for example:tellJokePrompt := mcp.NewPrompt("tell_joke", mcp.WithPromptArgument("adjective", mcp.ArgumentCompletion(handleCompleteFromDictionary)), mcp.WithPromptArgument("subject", mcp.Required(), mcp.ArgumentCompletion(handleCompleteFromDictionary)), )
201-217
: Demonstrate use ofContext
in completion handler.
TheCompleteRequest
now carries aContext
field to enable stateful suggestions, but this example ignoresreq.Params.Context
. Enhance the handler to read fromreq.Params.Context
(e.g., previously selected arguments) and show how it steers or filters the returned suggestions.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
mcp/prompts.go
(2 hunks)mcp/types.go
(6 hunks)www/docs/pages/servers/prompts.mdx
(1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
- mcp/prompts.go
- mcp/types.go
Description
Fixes #417
compeltion/complete
Context
field to theCompleteRequest
Example usage:
Type of Change
Checklist
MCP Spec Compliance
Additional Information
CompletionHandlerFunc
in themcp
package instead of theserver
package to avoid a circular import. Any suggestions on how to solve this with a cleaner approach are welcome.Summary by CodeRabbit
Summary by CodeRabbit