Skip to main content

Simple and fast framework to use MCP servers natively with AI clients.

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:

  1. list_tools - call the MCP server over http to get the tool definitions, then map them to AI library tool definitions
  2. `process_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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mcphero-0.0.2.tar.gz (94.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mcphero-0.0.2-py3-none-any.whl (13.0 kB view details)

Uploaded Python 3

File details

Details for the file mcphero-0.0.2.tar.gz.

File metadata

  • Download URL: mcphero-0.0.2.tar.gz
  • Upload date:
  • Size: 94.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for mcphero-0.0.2.tar.gz
Algorithm Hash digest
SHA256 16e8a99cf92bd1270b06406775f6d0dc56de81f337b98cbfb85b0c290c9ebe9f
MD5 deb520177c9705a3ec17756669711ca6
BLAKE2b-256 5c435feda43fa9423bb55fdc0af05ec3030d622e1242438623990f5e1814504b

See more details on using hashes here.

File details

Details for the file mcphero-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: mcphero-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 13.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for mcphero-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b0c1f4e4b376544f10f63f401a9e775e8f5d18002a2258c4205830ec11a72a49
MD5 0beaf89ebd3474d6e64f35c68c8ccc82
BLAKE2b-256 dff0a0be09c490da75aa6b03825560af21606b439442cef8db3e921718791762

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page