From b8fb2a439caf36fff6ba2109564a8a8fbbc67209 Mon Sep 17 00:00:00 2001 From: Matthew Peveler Date: Fri, 5 Sep 2025 09:28:17 -0600 Subject: [PATCH 1/3] Update MCP tool call customisation example Signed-off-by: Matthew Peveler --- docs/mcp/client.md | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/docs/mcp/client.md b/docs/mcp/client.md index cc44bd4f0e..aaad39d349 100644 --- a/docs/mcp/client.md +++ b/docs/mcp/client.md @@ -169,7 +169,39 @@ The MCP servers provide the ability to set a `process_tool_call` which allows the customisation of tool call requests and their responses. A common use case for this is to inject metadata to the requests which the server -call needs. +call needs. Accessing the metadata is then SDK specific, for example with the +MCP Python SDK, it is accessible via the +[Context](https://github.com/modelcontextprotocol/python-sdk#context) parameter +for tool calls. + +```python {title="mcp_server.py"} +from typing import Any + +from mcp.server.fastmcp import Context, FastMCP +from mcp.server.session import ServerSession + +mcp = FastMCP('Pydantic AI MCP Server') +log_level = 'unset' + + +@mcp.tool() +async def echo_deps(ctx: Context[ServerSession, None]) -> dict[str, Any]: + """Echo the run context. + + Args: + ctx: Context object containing request and session information. + + Returns: + Dictionary with an echo message and the deps. + """ + await ctx.info('This is an info message') + + deps: Any = getattr(ctx.request_context.meta, 'deps') + return {'echo': 'This is an echo message', 'deps': deps} + +if __name__ == '__main__': + mcp.run() +``` ```python {title="mcp_process_tool_call.py"} from typing import Any From a064861fbc19fe9a6f6eae6b8605c49c5fbe4f40 Mon Sep 17 00:00:00 2001 From: Matthew Peveler Date: Thu, 11 Sep 2025 22:58:32 -0600 Subject: [PATCH 2/3] feedback --- docs/mcp/client.md | 68 ++++++++++++++++++++++++---------------------- 1 file changed, 35 insertions(+), 33 deletions(-) diff --git a/docs/mcp/client.md b/docs/mcp/client.md index aaad39d349..e26ab47348 100644 --- a/docs/mcp/client.md +++ b/docs/mcp/client.md @@ -169,39 +169,7 @@ The MCP servers provide the ability to set a `process_tool_call` which allows the customisation of tool call requests and their responses. A common use case for this is to inject metadata to the requests which the server -call needs. Accessing the metadata is then SDK specific, for example with the -MCP Python SDK, it is accessible via the -[Context](https://github.com/modelcontextprotocol/python-sdk#context) parameter -for tool calls. - -```python {title="mcp_server.py"} -from typing import Any - -from mcp.server.fastmcp import Context, FastMCP -from mcp.server.session import ServerSession - -mcp = FastMCP('Pydantic AI MCP Server') -log_level = 'unset' - - -@mcp.tool() -async def echo_deps(ctx: Context[ServerSession, None]) -> dict[str, Any]: - """Echo the run context. - - Args: - ctx: Context object containing request and session information. - - Returns: - Dictionary with an echo message and the deps. - """ - await ctx.info('This is an info message') - - deps: Any = getattr(ctx.request_context.meta, 'deps') - return {'echo': 'This is an echo message', 'deps': deps} - -if __name__ == '__main__': - mcp.run() -``` +call needs: ```python {title="mcp_process_tool_call.py"} from typing import Any @@ -236,6 +204,40 @@ async def main(): #> {"echo_deps":{"echo":"This is an echo message","deps":42}} ``` +How to access the metadata is MCP SDK specific. For example with the MCP Python +SDK, it is accessible via the +[Context](https://github.com/modelcontextprotocol/python-sdk#context) +parameter that can be injected into tool call handlers: + +```python {title="mcp_server.py"} +from typing import Any + +from mcp.server.fastmcp import Context, FastMCP +from mcp.server.session import ServerSession + +mcp = FastMCP('Pydantic AI MCP Server') +log_level = 'unset' + + +@mcp.tool() +async def echo_deps(ctx: Context[ServerSession, None]) -> dict[str, Any]: + """Echo the run context. + + Args: + ctx: Context object containing request and session information. + + Returns: + Dictionary with an echo message and the deps. + """ + await ctx.info('This is an info message') + + deps: Any = getattr(ctx.request_context.meta, 'deps') + return {'echo': 'This is an echo message', 'deps': deps} + +if __name__ == '__main__': + mcp.run() +``` + ## Using Tool Prefixes to Avoid Naming Conflicts When connecting to multiple MCP servers that might provide tools with the same name, you can use the `tool_prefix` parameter to avoid naming conflicts. This parameter adds a prefix to all tool names from a specific server. From 3e9ac1861371b37d9117b01f9fdf598cfa8983fb Mon Sep 17 00:00:00 2001 From: Douwe Maan Date: Fri, 12 Sep 2025 11:14:49 -0600 Subject: [PATCH 3/3] Update docs/mcp/client.md --- docs/mcp/client.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/mcp/client.md b/docs/mcp/client.md index e26ab47348..c382bd07cf 100644 --- a/docs/mcp/client.md +++ b/docs/mcp/client.md @@ -204,10 +204,10 @@ async def main(): #> {"echo_deps":{"echo":"This is an echo message","deps":42}} ``` -How to access the metadata is MCP SDK specific. For example with the MCP Python -SDK, it is accessible via the -[Context](https://github.com/modelcontextprotocol/python-sdk#context) -parameter that can be injected into tool call handlers: +How to access the metadata is MCP server SDK specific. For example with the [MCP Python +SDK](https://github.com/modelcontextprotocol/python-sdk), it is accessible via the +[`ctx: Context`](https://github.com/modelcontextprotocol/python-sdk#context) +argument that can be included on tool call handlers: ```python {title="mcp_server.py"} from typing import Any