Library to use MCP servers natively with AI clients as tools.
Project description
MCPHero - MCP as tools / MCP as functions
Library to use MCP as tools / functions in native AI libraries
Inspiration
Everyone uses MCP now, but many still use old-school AI clients with no MCP support. These client libraries like openai or google-genai only have tool/function calls support.
This project is created to easily connect MCP servers to these libs as tools.
Concept
Two main flows:
list_tools- call the MCP server over http to get the tool definitions, then map them to AI library tool definitionsprocess_tool_calls- get the AI library's tool_calls, parse them, send the requests to mcp servers, return results
Installation
OpenAI (default) support:
pip install mcphero
For Google Gemini support:
pip install "mcphero[google-genai]"
Quick Start
OpenAI
import asyncio
from openai import OpenAI
from mcphero.adapters.openai import MCPToolAdapterOpenAI
async def main():
adapter = MCPToolAdapterOpenAI("https://api.mcphero.app/mcp/your-server-id")
client = OpenAI()
# Get tool definitions
tools = await adapter.get_tool_definitions()
# Make request with tools
messages = [{"role": "user", "content": "What's the weather in London?"}]
response = client.chat.completions.create(
model="gpt-4o",
messages=messages,
tools=tools,
)
# Process tool calls if present
if response.choices[0].message.tool_calls:
tool_results = await adapter.process_tool_calls(
response.choices[0].message.tool_calls
)
# Continue conversation with results
messages.append(response.choices[0].message)
messages.extend(tool_results)
final_response = client.chat.completions.create(
model="gpt-4o",
messages=messages,
tools=tools,
)
print(final_response.choices[0].message.content)
asyncio.run(main())
Google Gemini
import asyncio
from google import genai
from google.genai import types
from mcphero.adapters.gemini import MCPToolAdapterGemini
async def main():
adapter = MCPToolAdapterGemini("https://api.mcphero.app/mcp/your-server-id")
client = genai.Client(api_key="your-api-key")
# Get tool definitions
tool = await adapter.get_tool()
# Make request with tools
response = client.models.generate_content(
model="gemini-2.5-flash",
contents="What's the weather in London?",
config=types.GenerateContentConfig(
tools=[tool],
automatic_function_calling=types.AutomaticFunctionCallingConfig(
disable=True
),
),
)
# Process function calls if present
if response.function_calls:
results = await adapter.process_function_calls(response.function_calls)
# Continue conversation with results
contents = [
types.Content(role="user", parts=[types.Part.from_text("What's the weather in London?")]),
response.candidates[0].content,
*results,
]
final_response = client.models.generate_content(
model="gemini-2.5-flash",
contents=contents,
config=types.GenerateContentConfig(tools=[tool]),
)
print(final_response.text)
asyncio.run(main())
API Reference
MCPToolAdapterOpenAI
from mcphero.adapters.openai import MCPToolAdapterOpenAI
adapter = MCPToolAdapterOpenAI(
base_url="https://api.mcphero.app/mcp/your-server-id",
timeout=30.0, # optional
headers={"Authorization": "Bearer ..."}, # optional
)
Methods
| Method | Returns | Description |
|---|---|---|
get_tool_definitions() |
list[ChatCompletionToolParam] |
Fetch tools from MCP server as OpenAI tool schemas |
process_tool_calls(tool_calls, return_errors=True) |
list[ChatCompletionToolMessageParam] |
Execute tool calls and return results for the conversation |
MCPToolAdapterGemini
from mcphero.adapters.gemini import MCPToolAdapterGemini
adapter = MCPToolAdapterGemini(
base_url="https://api.mcphero.app/mcp/your-server-id",
timeout=30.0, # optional
headers={"Authorization": "Bearer ..."}, # optional
)
Methods
| Method | Returns | Description |
|---|---|---|
get_function_declarations() |
list[types.FunctionDeclaration] |
Fetch tools as Gemini FunctionDeclaration objects |
get_tool() |
types.Tool |
Fetch tools as a Gemini Tool object |
process_function_calls(function_calls, return_errors=True) |
list[types.Content] |
Execute function calls and return Content objects |
process_function_calls_as_parts(function_calls, return_errors=True) |
list[types.Part] |
Execute function calls and return Part objects |
Error Handling
Both adapters handle errors gracefully. When return_errors=True (default), failed tool calls return error messages that can be sent back to the model:
# Tool call fails -> returns error in result
results = await adapter.process_tool_calls(tool_calls, return_errors=True)
# [{"role": "tool", "tool_call_id": "...", "content": "{\"error\": \"HTTP error...\"}"}]
# Skip failed calls
results = await adapter.process_tool_calls(tool_calls, return_errors=False)
Links
License
MIT
Need a custom MCP server? Or a good, no bloat MCP server? Visit MCPHero and create one!
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 mcphero-0.2.0.tar.gz.
File metadata
- Download URL: mcphero-0.2.0.tar.gz
- Upload date:
- Size: 98.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2028208ea5f326ca1fa55df05af15dc17d3843d61f04b9f8da00ef01696e7035
|
|
| MD5 |
b89f6a9aedabab7caa2354dc4dbcbc64
|
|
| BLAKE2b-256 |
6f619cf5573bb900f4c84c6999dad2e8ed6101101079a3a19d3c5b708e8ce097
|
File details
Details for the file mcphero-0.2.0-py3-none-any.whl.
File metadata
- Download URL: mcphero-0.2.0-py3-none-any.whl
- Upload date:
- Size: 14.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
98039b246ba4d5cf2b9f293a9b2aa333b386679260871567a4837cc567ade7eb
|
|
| MD5 |
bd7381d9642416e2dcbb7d477bf1072e
|
|
| BLAKE2b-256 |
eb8e4ef744e8b002b87431046cc04ff223d783659c55ab2a3284e435dd8a840a
|