Simple Python library to connect LLM models with MCP (Model Context Protocol) servers
Project description
mcp-runtime
Simple Python library to connect LLM models with MCP (Model Context Protocol) servers
mcp-runtime provides a clean, easy-to-use API for developers to integrate MCP servers with their LLM models. It handles tools, resources, and prompts from any MCP server.
Features
- ✅ Simple API - Easy-to-use
MCPClientclass - ✅ LLM Adapters - Support for OpenAI, Gemini, and Claude
- ✅ Transport Options - Local (stdio) and remote (HTTP/SSE) MCP servers
- ✅ Full MCP Support - Tools, resources, and prompts
- ✅ Blueprint Caching - Save/load server capabilities for faster startup
- ✅ Optimized Performance - Schema caching, tool format caching
- ✅ Hosted Server Support - Works with GitHub MCP, NeonDB MCP, and any HTTP/SSE server
- ✅ Type Safe - Full type hints and proper error handling
Installation
pip install mcp-runtime
For LLM adapter support:
# OpenAI
pip install mcp-runtime[openai]
# Gemini
pip install mcp-runtime[gemini]
# Claude
pip install mcp-runtime[claude]
Quick Start
Basic Usage
import asyncio
from runtime import MCPClient
async def main():
# Connect to a local MCP server
client = MCPClient.local(
command=["python", "-m", "my_mcp_server"],
adapter="openai" # or "gemini", "claude"
)
# Connect and discover capabilities
await client.connect()
# Get tools for your LLM
tools = client.get_tools()
# Execute a tool
result = await client.call_tool("calculator", {
"operation": "multiply",
"a": 15,
"b": 23
})
print(result.content)
# Disconnect
await client.disconnect()
asyncio.run(main())
Connect to Remote MCP Server
# Connect to a remote MCP server (HTTP/SSE)
client = MCPClient.remote(
base_url="https://example.com/mcp",
adapter="openai",
headers={"Authorization": "Bearer token"}
)
await client.connect()
Using with LLM APIs
import asyncio
from runtime import MCPClient
import google.generativeai as genai # or openai, anthropic
async def main():
# Create MCP client
client = MCPClient.local(
command=["python", "-m", "my_mcp_server"],
adapter="gemini" # Optimized for Gemini!
)
await client.connect()
# Get tools in LLM format (cached for performance)
tools = client.get_tools()
# Configure Gemini with tools
model = genai.GenerativeModel(
model_name="gemini-pro",
tools=tools
)
# Make request
response = model.generate_content("Calculate 15 * 23")
# Parse tool calls from response
tool_calls = client.parse_tool_calls(response)
# Execute tool calls
for tool_call in tool_calls:
result = await client.call_tool(
tool_call["name"],
tool_call["arguments"]
)
# Format result for Gemini
tool_result = client.format_tool_result(
tool_call["name"],
result
)
# Continue conversation with tool result
# ...
await client.disconnect()
asyncio.run(main())
Blueprint Caching (Save/Load Capabilities)
Save server capabilities once, reuse them later for faster startup:
from runtime import MCPClient
# First run: Discover and save
client = MCPClient.local(
command=["python", "-m", "my_mcp_server"],
adapter="gemini"
)
await client.connect()
# Save blueprint for future use
blueprint_path = client.save_blueprint()
print(f"Saved to: {blueprint_path}")
# Second run: Load from blueprint (much faster!)
client2 = MCPClient.local(
command=["python", "-m", "my_mcp_server"],
adapter="gemini"
)
await client2.connect(use_blueprint=blueprint_path) # Skips discovery!
# Tools are ready immediately
tools = client2.get_tools()
Hosted MCP Servers
Works with any hosted MCP server (GitHub, NeonDB, etc.):
# Connect to hosted server
client = MCPClient.remote(
base_url="https://your-mcp-server.com/mcp",
adapter="gemini",
headers={"Authorization": "Bearer YOUR_TOKEN"}
)
await client.connect()
# Save blueprint for this hosted server
blueprint_path = client.save_blueprint()
# Next time, load from blueprint
await client.connect(use_blueprint=blueprint_path)
API Reference
MCPClient
Main client class for connecting to MCP servers.
Methods
connect()- Connect to MCP server and discover capabilitiesdisconnect()- Disconnect from MCP serverget_tools()- Get tools in LLM formatcall_tool(name, arguments)- Execute a tool callread_resource(name, uri_arguments)- Read a resourceexpand_prompt(name, arguments)- Expand a prompt templateparse_tool_calls(llm_response)- Parse tool calls from LLM responseformat_tool_result(tool_name, result)- Format tool result for LLM
Properties
server_info- Server informationtools- List of discovered toolsresources- List of discovered resourcesprompts- List of discovered promptsis_connected- Connection status
Factory Methods
MCPClient.local(command, adapter, ...)- Create client for local MCP serverMCPClient.remote(base_url, adapter, ...)- Create client for remote MCP server
Examples
See examples/example.py for a complete working example.
Architecture
mcp-runtime/
│
├── runtime/
│ ├── client.py # Main MCPClient class
│ ├── runtime.py # Core runtime (advanced usage)
│ ├── transport/ # Transport implementations
│ ├── adapter/ # LLM adapters
│ └── execution/ # Tool/resource/prompt execution
│
└── schemas/
└── mcp_types.py # Type definitions
Supported LLM Providers
- OpenAI - GPT-4, GPT-3.5 with function calling
- Google Gemini - Gemini Pro with function calling
- Anthropic Claude - Claude with tool use
Transport Options
- StdioTransport - Local MCP servers via subprocess
- HttpSSETransport - Remote MCP servers via HTTP/SSE
Error Handling
All errors inherit from MCPRuntimeError:
from runtime import MCPRuntimeError, TransportError, ExecutionError
try:
await client.call_tool("calculator", {"operation": "divide", "a": 10, "b": 0})
except ExecutionError as e:
print(f"Tool execution failed: {e}")
License
MIT
Contributing
Contributions welcome! Please ensure:
- Code follows the existing style
- Type hints included
- Error handling is explicit
- Tests included (if applicable)
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 mcp_runtime-0.1.4.tar.gz.
File metadata
- Download URL: mcp_runtime-0.1.4.tar.gz
- Upload date:
- Size: 31.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9b851be6f1e5bad346246ef46818013ab43c98a8a37d5a43516cb83e0ed7b53f
|
|
| MD5 |
e0fb7a25c605c116eaedb63ac783acc2
|
|
| BLAKE2b-256 |
34fa1e034ad3e6536bbe3b91678d53defa3519919518c5023c9865443e73b6dd
|
File details
Details for the file mcp_runtime-0.1.4-py3-none-any.whl.
File metadata
- Download URL: mcp_runtime-0.1.4-py3-none-any.whl
- Upload date:
- Size: 41.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d8cc61f442e5f721591fbb3866f46231ddf70abe3df744228178b57a83dc2404
|
|
| MD5 |
a9c36d3f73430a2a68abb4b240f5032a
|
|
| BLAKE2b-256 |
20d82583f664efd151fbaa3822fe0254e719fa9a0c1982cc8eba73ca3ea09b4c
|