Skip to content

Commit e896eda

Browse files
committed
Update MCP tool call customisation example
Signed-off-by: Matthew Peveler <[email protected]>
1 parent 55c6814 commit e896eda

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

docs/mcp/client.md

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

171171
A common use case for this is to inject metadata to the requests which the server
172-
call needs.
172+
call needs. Accessing the metadata is then SDK specific, for example with FastMCP
173+
it is stored in [Context](https://gofastmcp.com/servers/context) which can be
174+
injected into the tool handler.
175+
176+
```python {title="mcp_server.py"}
177+
from typing import Any
178+
179+
from mcp.server.fastmcp import Context, FastMCP
180+
from mcp.server.session import ServerSession
181+
182+
mcp = FastMCP('Pydantic AI MCP Server')
183+
log_level = 'unset'
184+
185+
186+
@mcp.tool()
187+
async def echo_deps(ctx: Context[ServerSession, None]) -> dict[str, Any]:
188+
"""Echo the run context.
189+
190+
Args:
191+
ctx: Context object containing request and session information.
192+
193+
Returns:
194+
Dictionary with an echo message and the deps.
195+
"""
196+
await ctx.info('This is an info message')
197+
198+
deps: Any = getattr(ctx.request_context.meta, 'deps')
199+
return {'echo': 'This is an echo message', 'deps': deps}
200+
201+
if __name__ == '__main__':
202+
mcp.run()
203+
```
173204

174205
```python {title="mcp_process_tool_call.py"}
175206
from typing import Any

0 commit comments

Comments
 (0)