# Monty MCP

**Execute any [MCP](https://modelcontextprotocol.io/) server in code mode.** Point Monty MCP at an MCP server and an agent can run Python that calls that server’s tools, with type-checked stubs generated from the tool schemas. Built on top of [Monty](https://github.com/pydantic/monty) for secure Python execution.

## Motivation

- **Anthropic** ([code execution](https://docs.anthropic.com/en/docs/agents-and-tools/tool-use/code-execution-tool), [programmatic tool calling](https://docs.anthropic.com/docs/en/agents-and-tools/tool-use/programmatic-tool-calling)): When the agent runs code that calls tools in one go, it cuts round-trips and tokens. Only final results enter context, and multi-tool workflows can see large token savings.
- **Cloudflare** ([Code Mode](https://blog.cloudflare.com/code-mode/)): When agents emit code instead of JSON, they get composition (loops, conditionals, multiple tools in one shot), better success rates, and large token reductions versus tool-call chains.
- Monty MCP brings the same idea to any MCP server: you point it at a server and the agent runs Python that calls that server’s tools.

## How it works

1. You provide your MCP server config.
2. The tool-exec server uses that config to connect to your server, list available tools, and expose code mode as one MCP tool: `execute(code: str)`.
3. The agent calls `execute` with Python code; Monty runs that code with backend tools injected as callables, and the final result is returned to the agent.

See `src/demo` for a full example.

## Quickstart

Run the demo agent:

```bash
uv run --extra demo monty-mcp
```

## Development

- Type checking: `uv run mypy src/monty_mcp src/demo`
- Linting: `uv run ruff check .`
- Formatting check: `uv run ruff format --check .`
- Tests: `uv run pytest`
