Skip to main content

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:

  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.2.1.tar.gz (97.9 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.2.1-py3-none-any.whl (14.4 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for mcphero-0.2.1.tar.gz
Algorithm Hash digest
SHA256 4f69d706867df4b62ef9d8dbea3123fab06a2a5be06f0600228a1ed87bfb9242
MD5 9312fe422a990818e3e7ea803e5d9918
BLAKE2b-256 81c409fd1f8c60aa108997186c95b9247499935440c11365dbf725830644114b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: mcphero-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 14.4 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.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a95b1ec0ad99c01abbfb39084fe385f4db927c93f00601a39c4ddc25fc142746
MD5 b199ea2968207bff758ee85da010e172
BLAKE2b-256 bf11538c12a6d198fad40717cc52c0583d81574d49beb6e7238111944bfddaf3

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