-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Add meta to Client methods
#1923
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
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
157eeed
Add meta to `Client` methods
Kludex 54fcaa3
Add migration note
Kludex 384e82c
update
Kludex 6ae2481
update
Kludex 4880604
bump
Kludex fc56716
bump typing-extensions
Kludex b9a9838
Add meta
Kludex 70d5e88
add note on migration
Kludex 32e4d07
merge
Kludex File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changes here are not breaking change. |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,27 +2,16 @@ | |
|
|
||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
| from contextlib import AsyncExitStack | ||
| from typing import Any | ||
|
|
||
| from pydantic import AnyUrl | ||
|
|
||
| import mcp.types as types | ||
| from mcp.client._memory import InMemoryTransport | ||
| from mcp.client.session import ( | ||
| ClientSession, | ||
| ElicitationFnT, | ||
| ListRootsFnT, | ||
| LoggingFnT, | ||
| MessageHandlerFnT, | ||
| SamplingFnT, | ||
| ) | ||
| from mcp.client.session import ClientSession, ElicitationFnT, ListRootsFnT, LoggingFnT, MessageHandlerFnT, SamplingFnT | ||
| from mcp.server import Server | ||
| from mcp.server.fastmcp import FastMCP | ||
| from mcp.shared.session import ProgressFnT | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
||
| from mcp.types._types import RequestParamsMeta | ||
|
|
||
|
|
||
| class Client: | ||
|
|
@@ -42,8 +31,11 @@ class Client: | |
| def add(a: int, b: int) -> int: | ||
| return a + b | ||
|
|
||
| async with Client(server) as client: | ||
| result = await client.call_tool("add", {"a": 1, "b": 2}) | ||
| async def main(): | ||
| async with Client(server) as client: | ||
| result = await client.call_tool("add", {"a": 1, "b": 2}) | ||
|
|
||
| asyncio.run(main()) | ||
| ``` | ||
| """ | ||
|
|
||
|
|
@@ -150,9 +142,9 @@ def server_capabilities(self) -> types.ServerCapabilities | None: | |
| """The server capabilities received during initialization, or None if not yet initialized.""" | ||
| return self.session.get_server_capabilities() | ||
|
|
||
| async def send_ping(self) -> types.EmptyResult: | ||
| async def send_ping(self, *, meta: RequestParamsMeta | None = None) -> types.EmptyResult: | ||
| """Send a ping request to the server.""" | ||
| return await self.session.send_ping() | ||
| return await self.session.send_ping(meta=meta) | ||
|
|
||
| async def send_progress_notification( | ||
| self, | ||
|
|
@@ -169,36 +161,54 @@ async def send_progress_notification( | |
| message=message, | ||
| ) | ||
|
|
||
| async def set_logging_level(self, level: types.LoggingLevel) -> types.EmptyResult: | ||
| async def set_logging_level( | ||
| self, | ||
| level: types.LoggingLevel, | ||
| *, | ||
| meta: RequestParamsMeta | None = None, | ||
| ) -> types.EmptyResult: | ||
| """Set the logging level on the server.""" | ||
| return await self.session.set_logging_level(level) | ||
| return await self.session.set_logging_level(level=level, meta=meta) | ||
|
|
||
| async def list_resources(self, *, cursor: str | None = None) -> types.ListResourcesResult: | ||
| async def list_resources( | ||
| self, | ||
| *, | ||
| cursor: str | None = None, | ||
| meta: RequestParamsMeta | None = None, | ||
| ) -> types.ListResourcesResult: | ||
| """List available resources from the server.""" | ||
| return await self.session.list_resources(params=types.PaginatedRequestParams(cursor=cursor)) | ||
| return await self.session.list_resources(params=types.PaginatedRequestParams(cursor=cursor, _meta=meta)) | ||
|
|
||
| async def list_resource_templates(self, *, cursor: str | None = None) -> types.ListResourceTemplatesResult: | ||
| async def list_resource_templates( | ||
| self, | ||
| *, | ||
| cursor: str | None = None, | ||
| meta: RequestParamsMeta | None = None, | ||
| ) -> types.ListResourceTemplatesResult: | ||
| """List available resource templates from the server.""" | ||
| return await self.session.list_resource_templates(params=types.PaginatedRequestParams(cursor=cursor)) | ||
| return await self.session.list_resource_templates( | ||
| params=types.PaginatedRequestParams(cursor=cursor, _meta=meta) | ||
| ) | ||
|
|
||
| async def read_resource(self, uri: str | AnyUrl) -> types.ReadResourceResult: | ||
| async def read_resource(self, uri: str, *, meta: RequestParamsMeta | None = None) -> types.ReadResourceResult: | ||
| """Read a resource from the server. | ||
|
|
||
| Args: | ||
| uri: The URI of the resource to read. | ||
| meta: Additional metadata for the request | ||
|
|
||
| Returns: | ||
| The resource content. | ||
| """ | ||
| return await self.session.read_resource(uri) | ||
| return await self.session.read_resource(uri, meta=meta) | ||
|
|
||
| async def subscribe_resource(self, uri: str | AnyUrl) -> types.EmptyResult: | ||
| async def subscribe_resource(self, uri: str, *, meta: RequestParamsMeta | None = None) -> types.EmptyResult: | ||
| """Subscribe to resource updates.""" | ||
| return await self.session.subscribe_resource(uri) | ||
| return await self.session.subscribe_resource(uri, meta=meta) | ||
|
|
||
| async def unsubscribe_resource(self, uri: str | AnyUrl) -> types.EmptyResult: | ||
| async def unsubscribe_resource(self, uri: str, *, meta: RequestParamsMeta | None = None) -> types.EmptyResult: | ||
| """Unsubscribe from resource updates.""" | ||
| return await self.session.unsubscribe_resource(uri) | ||
| return await self.session.unsubscribe_resource(uri, meta=meta) | ||
|
|
||
| async def call_tool( | ||
| self, | ||
|
|
@@ -207,7 +217,7 @@ async def call_tool( | |
| read_timeout_seconds: float | None = None, | ||
| progress_callback: ProgressFnT | None = None, | ||
| *, | ||
| meta: dict[str, Any] | None = None, | ||
| meta: RequestParamsMeta | None = None, | ||
| ) -> types.CallToolResult: | ||
| """Call a tool on the server. | ||
|
|
||
|
|
@@ -229,21 +239,29 @@ async def call_tool( | |
| meta=meta, | ||
| ) | ||
|
|
||
| async def list_prompts(self, *, cursor: str | None = None) -> types.ListPromptsResult: | ||
| async def list_prompts( | ||
| self, | ||
| *, | ||
| cursor: str | None = None, | ||
| meta: RequestParamsMeta | None = None, | ||
| ) -> types.ListPromptsResult: | ||
| """List available prompts from the server.""" | ||
| return await self.session.list_prompts(params=types.PaginatedRequestParams(cursor=cursor)) | ||
| return await self.session.list_prompts(params=types.PaginatedRequestParams(cursor=cursor, _meta=meta)) | ||
|
|
||
| async def get_prompt(self, name: str, arguments: dict[str, str] | None = None) -> types.GetPromptResult: | ||
| async def get_prompt( | ||
| self, name: str, arguments: dict[str, str] | None = None, *, meta: RequestParamsMeta | None = None | ||
| ) -> types.GetPromptResult: | ||
| """Get a prompt from the server. | ||
|
|
||
| Args: | ||
| name: The name of the prompt | ||
| arguments: Arguments to pass to the prompt | ||
| meta: Additional metadata for the request | ||
|
|
||
| Returns: | ||
| The prompt content. | ||
| """ | ||
| return await self.session.get_prompt(name=name, arguments=arguments) | ||
| return await self.session.get_prompt(name=name, arguments=arguments, meta=meta) | ||
|
|
||
| async def complete( | ||
| self, | ||
|
|
||
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
Oops, something went wrong.
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.
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.
feels weird that it's key access but makes sense given you can add anything to meta. just weird vibes.