Skip to content

Commit d4b30fe

Browse files
vbardassmails
authored andcommitted
add langgraph server example (langchain-ai#20)
1 parent 0e634d3 commit d4b30fe

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,51 @@ async with MultiServerMCPClient(
142142
math_response = await agent.ainvoke({"messages": "what's (3 + 5) x 12?"})
143143
weather_response = await agent.ainvoke({"messages": "what is the weather in nyc?"})
144144
```
145+
146+
## Using with LangGraph API Server
147+
148+
> [!TIP]
149+
> Check out [this guide](https://langchain-ai.github.io/langgraph/tutorials/langgraph-platform/local-server/) on getting started with LangGraph API server.
150+
151+
If you want to run a LangGraph agent that uses MCP tools in a LangGraph API server, you can use the following setup:
152+
153+
```python
154+
# graph.py
155+
from contextlib import asynccontextmanager
156+
from langchain_mcp_adapters.client import MultiServerMCPClient
157+
from langgraph.prebuilt import create_react_agent
158+
from langchain_anthropic import ChatAnthropic
159+
160+
model = ChatAnthropic(model="claude-3-5-sonnet-latest")
161+
162+
@asynccontextmanager
163+
async def make_graph():
164+
async with MultiServerMCPClient(
165+
{
166+
"math": {
167+
"command": "python",
168+
# Make sure to update to the full absolute path to your math_server.py file
169+
"args": ["/path/to/math_server.py"],
170+
"transport": "stdio",
171+
},
172+
"weather": {
173+
# make sure you start your weather server on port 8000
174+
"url": "http://localhost:8000/sse",
175+
"transport": "sse",
176+
}
177+
}
178+
) as client:
179+
agent = create_react_agent(model, client.get_tools())
180+
yield agent
181+
```
182+
183+
In your [`langgraph.json`](https://langchain-ai.github.io/langgraph/cloud/reference/cli/#configuration-file) make sure to specify `make_graph` as your graph entrypoint:
184+
185+
```json
186+
{
187+
"dependencies": ["."],
188+
"graphs": {
189+
"agent": "./graph.py:make_graph"
190+
}
191+
}
192+
```

0 commit comments

Comments
 (0)