Skip to content

Include context into completions #966

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

Merged
merged 4 commits into from
Jun 17, 2025
Merged

Include context into completions #966

merged 4 commits into from
Jun 17, 2025

Conversation

ihrpr
Copy link
Contributor

@ihrpr ihrpr commented Jun 16, 2025

Details: modelcontextprotocol/modelcontextprotocol#598
Closes: #917


Implementation:

  • Added CompletionContext type with optional arguments field to store previously resolved parameters
  • Updated CompleteRequestParams to include optional context parameter
  • Modified client's complete() method to accept context_arguments parameter
  • Updated server completion handler signature to receive the context

API Improvements:

  • Added completion() decorator method to FastMCP for cleaner API (lowlevel already had it)

@ihrpr ihrpr requested review from dsp-ant and bhosmer-ant June 16, 2025 20:03
bhosmer-ant
bhosmer-ant previously approved these changes Jun 16, 2025
Copy link
Contributor

@bhosmer-ant bhosmer-ant left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LG! Couple stray late-in-the-day thoughts inline but prob not worth doing immediately

return Completion(values=["users_db", "products_db", "analytics_db"], total=3, hasMore=False)
elif argument.name == "table":
# Complete table names based on selected database
if context and context.arguments:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe overpolishing, but do we want a test where the server errors on not enough context (e.g. "need a database to complete tables" and the client receives it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you, added!

@@ -364,6 +364,24 @@ def decorator(fn: AnyFunction) -> AnyFunction:

return decorator

def completion(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it weird to route all completions to the same function in the high level API? Alternative would be to defer adding the fastmcp decorator for now and do something fancier that routes on URI, but agree that it's nice to bring fastmcp decorator coverage to parity with the lowlevel server.

Also a) maybe it's natural for "real" servers to route all completion requests through the same handler, and b) we could always add new decorators (resource_completion, prompt_completion) later.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, it is quite weird even on the lowlevel, TS has completions handling on a field that needs to be completed, which I found quite nice.

I'd love to make it nicer, but for now to have at least a way to handle completions on FastMCP level and not do to things like _server.

@ihrpr ihrpr merged commit a3bcabd into main Jun 17, 2025
12 checks passed
@ihrpr ihrpr deleted the ihrpr/context-completions branch June 17, 2025 08:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

include previously-resolved variables in completions request
2 participants