Skip to main content

Bidirectional adapter for converting between MCP and Opal tools

Project description

MCP-Opal Adapter

A bidirectional adapter service that converts between MCP (Model Context Protocol) tools and Opal tools using the official Opal Tools SDK with automatic MCP discovery.

Key Features

  • MCP Discovery: Automatically discovers tools from MCP servers
  • Proper Opal Tools SDK Integration: Uses ToolsService and @tool decorator
  • Dynamic Schema Translation: JSON Schema to Pydantic model conversion
  • JSON-RPC Proxy: Forwards MCP calls via JSON-RPC protocol
  • Discovery Endpoint: Exposes /discovery for tool listing
  • Just-in-Time Configuration: HTTP API for dynamic tool registration

Architecture

┌─────────────────────────────────────────────────────────────┐
│                    HTTP Configuration API                   │
│           POST /configure (MCP endpoint only)               │
└─────────────────────┬───────────────────────────────────────┘
                      │
┌─────────────────────▼───────────────────────────────────────┐
│                 MCP-Opal Proxy Service                      │
│  ┌─────────────────┐    ┌─────────────────┐                │
│  │   MCP Discovery │◄──►│   Opal Tools    │                │
│  │   (tools/list)  │    │   SDK           │                │
│  └─────────────────┘    └─────────────────┘                │
│  ┌─────────────────┐    ┌─────────────────┐                │
│  │  Dynamic Tool   │    │  Protocol       │                │
│  │  Registry       │    │  Translator     │                │
│  └─────────────────┘    └─────────────────┘                │
└─────────────────────────────────────────────────────────────┘

Quick Start

1. Build and Run

# Build the Docker image
docker build -t mcp-opal-adapter .

# Run with Docker Compose
docker-compose up -d

2. Configure MCP Tools via Discovery

# Configure tools by discovering them from an MCP server
curl -X POST http://localhost:8000/configure \
  -H "Content-Type: application/json" \
  -d '"http://your-mcp-server:3000"'

3. Discover Available Tools

# Get list of available Opal tools
curl http://localhost:8000/discovery

4. Call Tools

# Call a discovered tool
curl -X POST http://localhost:8000/tools/weather_lookup \
  -H "Content-Type: application/json" \
  -d '{"location": "New York", "units": "imperial"}'

API Reference

Configuration Endpoints

POST /configure

Configure MCP tools by discovering them from an MCP server.

Request Body:

"http://mcp-server:3000"

Response:

{
  "status": "configured",
  "tools": ["weather_lookup", "calculator"],
  "total_discovered": 2,
  "successfully_configured": 2
}

Opal Tools Endpoints

GET /discovery

Discover available Opal tools (auto-generated by Opal Tools SDK).

POST /tools/{tool_name}

Call a configured tool via Opal interface.

Request Body:

{
  "param1": "value1",
  "param2": 42
}

Management Endpoints

GET /health

Health check endpoint.

GET /status

Get adapter status and configuration.

DELETE /configure/{tool_name}

Remove a configured tool.

MCP Discovery Process

The adapter automatically:

  1. Discovers Tools: Calls tools/list on the MCP server
  2. Extracts Schemas: Gets tool names, descriptions, and input schemas
  3. Converts Schemas: Transforms JSON Schema to Pydantic models
  4. Creates Proxies: Generates proxy functions for each tool
  5. Registers Tools: Registers them with the Opal Tools SDK

MCP Protocol Support

The adapter supports the standard MCP protocol:

  • tools/list: Discovers available tools
  • tools/call: Executes tool calls via JSON-RPC
  • server/health: Health checking (optional)

Development

Local Development

# Install uv if not already installed
pip install uv

# Install dependencies using uv
uv sync

# For development with testing tools
uv sync --extra dev

# Run development server
uvicorn main:app --reload --host 0.0.0.0 --port 8000

Testing

# Run tests using uv
uv run pytest

# Or install dev dependencies and run tests
uv sync --extra dev
pytest

Development Workflow

# Install all dependencies including dev tools
make build-dev

# Run tests
make test

# Run development server
make run

Deployment

Docker Compose

version: '3.8'
services:
  mcp-opal-adapter:
    build: .
    ports:
      - "8000:8000"
    environment:
      - ADAPTER_PORT=8000
      - LOG_LEVEL=info
    networks:
      - adapter-network

Kubernetes

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mcp-opal-adapter
spec:
  replicas: 3
  selector:
    matchLabels:
      app: mcp-opal-adapter
  template:
    metadata:
      labels:
        app: mcp-opal-adapter
    spec:
      containers:
      - name: adapter
        image: mcp-opal-adapter:latest
        ports:
        - containerPort: 8000
        env:
        - name: ADAPTER_PORT
          value: "8000"
        - name: LOG_LEVEL
          value: "info"

Protocol Translation

MCP to Opal Flow

  1. Discover Tools: Call tools/list on MCP server
  2. Convert Schema: JSON Schema → Pydantic model using create_model()
  3. Create Proxy Function: Async function that forwards to MCP server
  4. Register with Opal SDK: Use @tool decorator to register
  5. Handle Calls: Proxy function forwards JSON-RPC calls to MCP server

JSON-RPC Proxy

# MCP JSON-RPC request format
{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "tools/call",
  "params": {
    "name": "tool_name",
    "arguments": {"param1": "value1"}
  }
}

Key Implementation Details

Opal Tools SDK Integration

from opal_tools_sdk import ToolsService, tool

# Initialize with FastAPI app
app = FastAPI()
tools_service = ToolsService(app)

# Register dynamic tool
tool_decorator = tool("tool_name", "description")
registered_func = tool_decorator(proxy_function)

Dynamic Pydantic Model Creation

from pydantic import create_model

# Convert JSON Schema to Pydantic model
model_class = create_model(
    "ToolParameters",
    **field_definitions
)

MCP Discovery Function

async def discover_mcp_tools(mcp_endpoint: str):
    # Call tools/list on MCP server
    discovery_request = {
        "jsonrpc": "2.0",
        "id": 1,
        "method": "tools/list",
        "params": {}
    }
    # Process response and extract tool information

Error Handling

Network Errors

  • Connection timeouts (30s default)
  • MCP server unavailability
  • JSON-RPC protocol errors

Validation Errors

  • Schema validation failures via Pydantic
  • Required field missing
  • Type conversion errors

Discovery Errors

  • MCP server discovery failures
  • Invalid tool schemas
  • Duplicate tool names

Monitoring

Health Checks

  • Service availability
  • Tool count monitoring
  • Configuration status

Logging

  • Structured logging throughout
  • Error tracking
  • Performance monitoring

Security Considerations

Input Validation

  • Schema-based validation via Pydantic
  • Type checking and conversion
  • Required field enforcement

Network Security

  • Timeout configuration
  • Error message sanitization
  • Connection pooling

Future Enhancements

Planned Features

  • Authentication and authorization
  • Rate limiting
  • Caching layer
  • Metrics collection
  • WebSocket support
  • GraphQL interface

Scalability Improvements

  • Database persistence
  • Load balancing
  • Service discovery
  • Circuit breaker pattern

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests
  5. Submit a pull request

License

MIT License - see LICENSE file for details.

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

opal_mcp_adapter-0.0.1.tar.gz (32.6 kB view details)

Uploaded Source

Built Distribution

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

opal_mcp_adapter-0.0.1-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

Details for the file opal_mcp_adapter-0.0.1.tar.gz.

File metadata

  • Download URL: opal_mcp_adapter-0.0.1.tar.gz
  • Upload date:
  • Size: 32.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.13

File hashes

Hashes for opal_mcp_adapter-0.0.1.tar.gz
Algorithm Hash digest
SHA256 f14a3c53dadaade0ebae4f8f9a92521848b8239fac65ca463f1a1cabac320fbf
MD5 73af0a1893b42b4c7de979b0928fc05f
BLAKE2b-256 e78757ef8fb6d187e67bd01684e1b425c43c8c609ae482cf6d520fe848e5fcd6

See more details on using hashes here.

File details

Details for the file opal_mcp_adapter-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for opal_mcp_adapter-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 94c6cb087ac35c68fea638ed3a0f10c0422f294b7e947dbc79be7677c5ccd357
MD5 355396a3279037193ddda8007051b114
BLAKE2b-256 9b63485ab610a7b314b98acabb85ed05a0be0c6f269b4ccd40c03e1501bb2f6c

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