Commit 092da2a
authored
feat: inject runtime arg for ToolRuntime access (#370)
Theoretically allows for access to tool call id, state, etc when
available in a LG context (like with `create_agent`).
This works w/ the new `langgraph-prebuilt` and `langchain-core`.
```py
import asyncio
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
from langchain.agents import create_agent
from langchain_mcp_adapters.interceptors import MCPToolCallRequest
from langchain_mcp_adapters.tools import load_mcp_tools
from langchain_openai import ChatOpenAI
from mcp.types import CallToolResult
from typing_extensions import TypedDict
async def main():
async def runtime_interceptor(
request: MCPToolCallRequest, handler
) -> CallToolResult:
print(request.runtime.tool_call_id)
print(request.runtime.context)
print(request.runtime.state)
return await handler(request)
server_params = StdioServerParameters(
command="python",
args=["./math_server.py"],
)
async with stdio_client(server_params) as (read, write):
async with ClientSession(read, write) as session:
await session.initialize()
tools = await load_mcp_tools(
session, tool_interceptors=[runtime_interceptor]
)
class ContextSchema(TypedDict):
user_id: str
agent = create_agent(
model=ChatOpenAI(model="gpt-4o-mini"),
tools=tools,
context_schema=ContextSchema,
)
await agent.ainvoke(
{"messages": [{"role": "user", "content": "What is 5 + 3?"}]},
context={"user_id": "123"},
)
if __name__ == "__main__":
asyncio.run(main())
"""
[11/21/25 10:02:42] INFO Processing request of type ListToolsRequest server.py:674
call_f3Unpu6pMXc3erYy2U540a8s
{'user_id': '123'}
{'messages': [HumanMessage(content='What is 5 + 3?', additional_kwargs={}, response_metadata={}, id='ca0e9e84-34a0-4540-b92f-bcf023316562'), AIMessage(content='', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 17, 'prompt_tokens': 73, 'total_tokens': 90, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}}, 'model_provider': 'openai', 'model_name': 'gpt-4o-mini-2024-07-18', 'system_fingerprint': 'fp_560af6e559', 'id': 'chatcmpl-CeMwlKviSagQxVwtkocmYe9YzNlRb', 'service_tier': 'default', 'finish_reason': 'tool_calls', 'logprobs': None}, id='lc_run--f8fff704-5f76-4526-8dbd-62878b5fbc15-0', tool_calls=[{'name': 'add', 'args': {'a': 5, 'b': 3}, 'id': 'call_f3Unpu6pMXc3erYy2U540a8s', 'type': 'tool_call'}], usage_metadata={'input_tokens': 73, 'output_tokens': 17, 'total_tokens': 90, 'input_token_details': {'audio': 0, 'cache_read': 0}, 'output_token_details': {'audio': 0, 'reasoning': 0}})]}
[11/21/25 10:02:44] INFO Processing request of type CallToolRequest
"""
```1 parent c6d6c56 commit 092da2a
File tree
3 files changed
+57
-22
lines changed- langchain_mcp_adapters
- tests
3 files changed
+57
-22
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| 15 | + | |
15 | 16 | | |
16 | 17 | | |
17 | 18 | | |
18 | 19 | | |
19 | 20 | | |
20 | 21 | | |
21 | | - | |
22 | | - | |
| 22 | + | |
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
| |||
35 | 36 | | |
36 | 37 | | |
37 | 38 | | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | 39 | | |
48 | 40 | | |
49 | 41 | | |
50 | 42 | | |
51 | 43 | | |
52 | 44 | | |
53 | | - | |
| 45 | + | |
54 | 46 | | |
55 | 47 | | |
56 | 48 | | |
57 | | - | |
| 49 | + | |
| 50 | + | |
58 | 51 | | |
59 | 52 | | |
60 | | - | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
61 | 57 | | |
62 | 58 | | |
63 | 59 | | |
64 | 60 | | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
65 | 67 | | |
66 | 68 | | |
67 | 69 | | |
| |||
184 | 186 | | |
185 | 187 | | |
186 | 188 | | |
| 189 | + | |
187 | 190 | | |
188 | | - | |
| 191 | + | |
189 | 192 | | |
190 | 193 | | |
191 | 194 | | |
| 195 | + | |
192 | 196 | | |
193 | 197 | | |
194 | 198 | | |
195 | | - | |
| 199 | + | |
| 200 | + | |
196 | 201 | | |
197 | 202 | | |
198 | 203 | | |
| |||
202 | 207 | | |
203 | 208 | | |
204 | 209 | | |
205 | | - | |
206 | | - | |
207 | | - | |
208 | | - | |
209 | | - | |
210 | | - | |
211 | 210 | | |
212 | 211 | | |
213 | 212 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
| |||
283 | 284 | | |
284 | 285 | | |
285 | 286 | | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
0 commit comments