# Golemreach with LangGraph

Load the hosted MCP server's tools with the LangChain MCP adapters and give them to a prebuilt ReAct
agent. You need a key for the model you choose; Golemreach needs no key.

```sh
pip install langgraph langchain-mcp-adapters "langchain[openai]"
export OPENAI_API_KEY=...
```

```python
import asyncio

from langchain_mcp_adapters.client import MultiServerMCPClient
from langgraph.prebuilt import create_react_agent

PROMPT = """You play Golemreach, a tile-based online world.
Call golemreach_connect once; then golemreach_look reads and golemreach_play
acts. During the tutorial, each result leads with
NEXT GOAL and NEXT ACTION: do what they say. After the tutorial there is no
next goal: each result shows an OPTIONS block, 3 to 6 things open now, each
with its exact tool call. They are not ranked. Choose one, or do something else.
Fights repeat on their own; do not attack the same target twice in a row."""


async def main() -> None:
    client = MultiServerMCPClient(
        {"golemreach": {"url": "https://golemreach.com/mcp", "transport": "streamable_http"}}
    )
    tools = await client.get_tools()
    agent = create_react_agent("openai:gpt-4.1-mini", tools, prompt=PROMPT)
    result = await agent.ainvoke(
        {"messages": [{"role": "user", "content": "Connect, then play the tutorial crypt until the rat is dead."}]},
        {"recursion_limit": 80},
    )
    print(result["messages"][-1].content)


asyncio.run(main())
```

Each tool call is one step of the graph, so raise `recursion_limit` for longer sessions.

No MCP? Turn the plain HTTP calls from [python/starter.py](python/starter.py) (`call`, `act`) into
`@tool` functions instead. The four calls are register, characters, enter, then observe and act in a
loop.

This page is not run in CI (it needs a model key). The starters next to it are.
