Generic Claude agent gateway with MCP tool support and streaming
Project description
claude-gateway
A Python package for building streaming Claude agent applications with MCP (Model Context Protocol) tool support. Handles the agent loop, tool dispatch, MCP server management, and SSE event streaming — so you can focus on your domain logic.
Install
pip install claude-gateway
Quick Start
import asyncio
from claude_gateway import AgentRunner, EventLog, McpClientManager, ToolDispatcher
async def main():
# 1. Set up MCP client (reads ~/.claude.json for server configs)
mcp = McpClientManager(
allowed_servers={"my-mcp-server"},
builtin_tool_names=set(),
)
await mcp.startup()
# 2. Create a tool dispatcher with local handlers + MCP tools
async def my_tool(tool_input, *, call_index=0):
return {"result": f"Hello from {tool_input.get('name', 'world')}"}, None
dispatcher = ToolDispatcher(
mcp_client=mcp,
local_tool_handlers={"greet": my_tool},
)
# 3. Create event log for SSE streaming
event_log = EventLog(session_id="session-001")
# 4. Run the agent
runner = AgentRunner(
event_log=event_log,
dispatcher=dispatcher,
session_id="session-001",
auth_config={"auth_mode": "api", "api_key": "sk-ant-..."},
mcp_client=mcp,
get_tool_definitions=lambda: [
{"name": "greet", "description": "Greet someone", "input_schema": {
"type": "object",
"properties": {"name": {"type": "string"}},
}},
*mcp.get_tool_definitions(),
],
)
# Start streaming (non-blocking, populates event_log)
asyncio.create_task(runner.run(
messages=[{"role": "user", "content": "Say hello"}],
system_prompt="You are a helpful assistant.",
))
# Consume SSE events
async for entry in event_log.iter_from():
print(entry.event)
if entry.event.get("type") == "stream_complete":
break
await mcp.shutdown()
asyncio.run(main())
API Reference
AgentRunner
Core streaming agent loop. Sends messages to Claude, processes tool calls, and emits SSE events.
- Hooks:
on_tool_result,on_usage,on_tool_timing— async callbacks for observability - Sub-agents:
sub_agent_config+spawn_sub_agent()for parallel task delegation - Streaming: Emits
thinking_delta,text_delta,tool_use,tool_result,stream_completeevents
ToolDispatcher
Routes tool calls to local handlers or MCP servers. Supports approval gates for sensitive operations.
local_tool_handlers:Dict[str, handler]— local async functionsneeds_approval/request_approval: Optional approval flow for gated toolsapproved_tool_types: Session-persistent set of pre-approved tools
McpClientManager
Manages MCP server connections via stdio. Reads server configs from ~/.claude.json.
allowed_servers: Whitelist of server names to connectbuiltin_tool_names: Tool names to skip (avoids collisions with local tools)timeout_overrides: Per-server call timeouts
EventLog
Async event stream for SSE. Append events, iterate from any sequence number.
append(event)— add an eventiter_from(after_seq)— async iterator, blocks until new events arriveclose()— emit terminal event and seal the log
Supporting Types
ToolResult = Tuple[Optional[Any], Optional[Dict[str, Any]]]—(result, error)pairApprovalRequest/ApprovalDecision— approval flow dataclassesSubAgentConfig— excluded tools, system prompt, max turns for sub-agentsToolResultContext— full context passed toon_tool_resulthookLogEntry— sequence number, timestamp, event dict
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ai_agent_gateway-0.2.1.tar.gz.
File metadata
- Download URL: ai_agent_gateway-0.2.1.tar.gz
- Upload date:
- Size: 22.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6d8730cd04be1eb50b0c634d5da33889c2e5bca97283525d56eaed8f32dfcee3
|
|
| MD5 |
404866c021bb39abe81e79bfa3cc02ba
|
|
| BLAKE2b-256 |
409a327d72783646e69498a1f1013f7390bc6f49acf0541cb67fb0595f9397db
|
File details
Details for the file ai_agent_gateway-0.2.1-py3-none-any.whl.
File metadata
- Download URL: ai_agent_gateway-0.2.1-py3-none-any.whl
- Upload date:
- Size: 24.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
06ae4dfc7e193e14615e7f220d832aad627a9853c38305d0d698fe512c7b557b
|
|
| MD5 |
b89744d3c87f1c36bb65d3644826ce0e
|
|
| BLAKE2b-256 |
2d0e250087bc013eae68081907c1e23879450167e30d45e9f2ed5e79c6bcefaf
|