Skip to main content

gopher-mcp-python Python SDK

Python SDK for gopher-mcp-python, providing AI agent orchestration with native C++ performance through ctypes FFI bindings.

Features

  • Python 3.8+ compatibility
  • pip/setuptools build system
  • ctypes FFI bindings to native library
  • GopherAgent class with builder pattern configuration
  • Context manager support for automatic resource cleanup
  • Typed errors (AgentError, ApiKeyError, ConnectionError, TimeoutError)
  • Comprehensive test suite with pytest

Requirements

  • Python 3.8 or higher
  • venv and pip for PyPI installation, examples, and development workflows
  • Native gopher-mcp-python library (built from source)
  • On Linux, system OpenSSL runtime libraries (libssl / libcrypto) from your distribution. Native wheels do not bundle OpenSSL, so OS security updates remain effective.

On Debian/Ubuntu, install the Python venv and pip packages before using the PyPI install path, running examples, or setting up development dependencies:

sudo apt-get install python3 python3-venv python3-pip

Installation

From Source

  1. Clone the repository:
git clone https://github.com/GopherSecurity/gopher-mcp-python.git
cd gopher-mcp-python
  1. Build the native library:
./build.sh
  1. Install the Python package:
python3 -m pip install -e .

Quick Start

Using API Key

from gopher_mcp_python import GopherAgent, GopherAgentConfig

# Create configuration with API key
config = (GopherAgentConfig.builder()
    .provider("AnthropicProvider")
    .model("claude-3-haiku-20240307")
    .api_key("your-api-key")
    .build())

# Create and use agent with context manager
with GopherAgent.create(config) as agent:
    response = agent.run("What time is it in Tokyo?")
    print(response)

Using JSON Server Configuration

from gopher_mcp_python import GopherAgent, GopherAgentConfig

# Create configuration with server config
config = (GopherAgentConfig.builder()
    .provider("AnthropicProvider")
    .model("claude-3-haiku-20240307")
    .server_config('{"mcpServers": [...]}')
    .build())

# Create agent
agent = GopherAgent.create(config)
try:
    response = agent.run("What is the weather?")
    print(response)
finally:
    agent.dispose()

Using Convenience Methods

from gopher_mcp_python import GopherAgent

# Create with API key (shorthand)
agent = GopherAgent.create_with_api_key(
    provider="AnthropicProvider",
    model="claude-3-haiku-20240307",
    api_key="your-api-key"
)

# Or with server config
agent = GopherAgent.create_with_server_config(
    provider="AnthropicProvider",
    model="claude-3-haiku-20240307",
    server_config='{"mcpServers": [...]}'
)

Getting Detailed Results

from gopher_mcp_python import GopherAgent

with GopherAgent.create(config) as agent:
    result = agent.run_detailed("What time is it?")

    if result.is_success():
        print(f"Response: {result.response}")
        print(f"Iterations: {result.iteration_count}")
        print(f"Tokens used: {result.tokens_used}")
    elif result.is_timeout():
        print(f"Request timed out: {result.error_message}")
    else:
        print(f"Error: {result.error_message}")

API Reference

GopherAgent

Main class for interacting with the gopher-mcp-python native library.

Static Methods

  • GopherAgent.init() - Initialize the library (called automatically)
  • GopherAgent.shutdown() - Shutdown the library
  • GopherAgent.is_initialized() - Check if library is initialized
  • GopherAgent.create(config) - Create an agent with configuration
  • GopherAgent.create_with_api_key(provider, model, api_key) - Convenience method
  • GopherAgent.create_with_server_config(provider, model, server_config) - Convenience method

Instance Methods

  • agent.run(query, timeout_ms=60000) - Run a query and get response string
  • agent.run_detailed(query, timeout_ms=60000) - Run a query and get AgentResult
  • agent.dispose() - Release resources
  • agent.is_disposed() - Check if agent is disposed

GopherAgentConfig

Configuration class with builder pattern.

config = (GopherAgentConfig.builder()
    .provider("AnthropicProvider")  # Required
    .model("claude-3-haiku-20240307")  # Required
    .api_key("key")  # Either api_key or server_config required
    .server_config("{...}")  # Either api_key or server_config required
    .build())

AgentResult

Result class with status and metadata.

  • result.response - Response text
  • result.status - AgentResultStatus enum
  • result.iteration_count - Number of iterations
  • result.tokens_used - Tokens consumed
  • result.error_message - Error message (if applicable)
  • result.is_success() - Check if successful
  • result.is_error() - Check if error
  • result.is_timeout() - Check if timeout

Exceptions

  • AgentError - Base exception for agent errors
  • ApiKeyError - Invalid API key
  • ConnectionError - Connection failed
  • TimeoutError - Operation timed out

Development

Running Tests

pytest

Code Formatting

This project uses Black for code formatting and Ruff for linting.

Format code:

black .

Check formatting without modifying:

black --check .

Run linter:

ruff check .

Fix linting issues:

ruff check --fix .

Building the Native Library

./build.sh

Clean Build

./build.sh --clean

Environment Variables

  • GOPHER_MCP_PYTHON_LIBRARY_PATH - Custom path to native library
  • DEBUG - Enable debug output for library loading

License

See LICENSE file for details.

Download files

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

Source Distribution

gopher_mcp_python-0.1.34.tar.gz (77.0 kB view details)

Uploaded Source

Built Distribution

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

gopher_mcp_python-0.1.34-py3-none-any.whl (66.4 kB view details)

Uploaded Python 3

File details

Details for the file gopher_mcp_python-0.1.34.tar.gz.

File metadata

  • Download URL: gopher_mcp_python-0.1.34.tar.gz
  • Upload date:
  • Size: 77.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.11.15

File hashes

Hashes for gopher_mcp_python-0.1.34.tar.gz
Algorithm Hash digest
SHA256 e13bd60cdb47cb3b2b461852cfd532ce12cc211a4596d9c0df87c985a1b9f8e8
MD5 abf3b0590c719cded748f7398305cc5f
BLAKE2b-256 1c70f7567b3472ffe5beadebe955ccd25243f3e0bddb1c301cad22574587e456

See more details on using hashes here.

File details

Details for the file gopher_mcp_python-0.1.34-py3-none-any.whl.

File metadata

File hashes

Hashes for gopher_mcp_python-0.1.34-py3-none-any.whl
Algorithm Hash digest
SHA256 97d282ef53eb1cf136e40cf2fbbb5ac0a8e021ba18c92b92eba957f327e87cc2
MD5 2bebc7d5d4be0a3b6e2c2c8ce7bfba2a
BLAKE2b-256 faea77b0aea378d38c17fc499c86a093758b6580858a4cbbc45fab985b68fa43

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 Sentry Error logging StatusPage Status page