Skip to content

Commit 773e1be

Browse files
MasterOdinDouweM
andauthored
Update MCP tool call customisation docs (#2817)
Signed-off-by: Matthew Peveler <[email protected]> Co-authored-by: Douwe Maan <[email protected]>
1 parent dcd7690 commit 773e1be

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

docs/mcp/client.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ The MCP servers provide the ability to set a `process_tool_call` which allows
222222
the customisation of tool call requests and their responses.
223223

224224
A common use case for this is to inject metadata to the requests which the server
225-
call needs.
225+
call needs:
226226

227227
```python {title="mcp_process_tool_call.py"}
228228
from typing import Any
@@ -257,6 +257,40 @@ async def main():
257257
#> {"echo_deps":{"echo":"This is an echo message","deps":42}}
258258
```
259259

260+
How to access the metadata is MCP server SDK specific. For example with the [MCP Python
261+
SDK](https://github.com/modelcontextprotocol/python-sdk), it is accessible via the
262+
[`ctx: Context`](https://github.com/modelcontextprotocol/python-sdk#context)
263+
argument that can be included on tool call handlers:
264+
265+
```python {title="mcp_server.py"}
266+
from typing import Any
267+
268+
from mcp.server.fastmcp import Context, FastMCP
269+
from mcp.server.session import ServerSession
270+
271+
mcp = FastMCP('Pydantic AI MCP Server')
272+
log_level = 'unset'
273+
274+
275+
@mcp.tool()
276+
async def echo_deps(ctx: Context[ServerSession, None]) -> dict[str, Any]:
277+
"""Echo the run context.
278+
279+
Args:
280+
ctx: Context object containing request and session information.
281+
282+
Returns:
283+
Dictionary with an echo message and the deps.
284+
"""
285+
await ctx.info('This is an info message')
286+
287+
deps: Any = getattr(ctx.request_context.meta, 'deps')
288+
return {'echo': 'This is an echo message', 'deps': deps}
289+
290+
if __name__ == '__main__':
291+
mcp.run()
292+
```
293+
260294
## Using Tool Prefixes to Avoid Naming Conflicts
261295

262296
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.

0 commit comments

Comments
 (0)