mcpo Documentation

repository·main·Indexed 26 days ago

https://github.com/open-webui/mcpo

A secure MCP-to-OpenAPI proxy server that exposes Model Context Protocol (MCP) tools as OpenAPI-compatible HTTP servers. It enables LLM agents and applications expecting standard RESTful APIs to interact with MCP servers via stdio, SSE, or Streamable HTTP. Features include support for OAuth 2.1 authentication, multi-server configuration via JSON files, hot reloading, and API key middleware for Bearer or Basic authentication.

Tokens
4.6K
Snippets
9
Records
34
Agent score
88%

What's inside mcpo

  1. Set up mcpo for development

    main

    To develop or test mcpo locally:

    1. Clone and Install:

      git clone https://github.com/open-webui/mcpo.git
      cd mcpo
      uv sync --dev
    2. Run Tests:

      uv run pytest
    3. Run with Local Changes: To run mcpo using your local modified code from a specific branch:

      git checkout <your-branch>
      uv run mcpo --port 8000 -- <your_mcp_server_command>
    uv run mcpo --port 8000 -- uvx mcp-server-time --local-timezone=America/New_York
  2. Use Automatic vs Manual OAuth Authorization Flows

    main

    MCPO supports two ways to complete the OAuth authorization process:

    1. Automatic Browser Flow (Default): Set use_loopback: true. MCPO opens your default browser to the authorization page. After authorization, you are redirected to http://localhost:PORT/callback, and MCPO's built-in server captures the code.
    2. Manual Copy/Paste Flow: Set use_loopback: false. MCPO prints the authorization URL. You must manually open the URL, authorize, and then copy the full callback URL (including query parameters) back into the MCPO prompt.
  3. Configure Basic OAuth 2.1 for MCP Servers

    main

    For most modern OAuth 2.1 servers, MCPO uses dynamic client registration. You only need to provide the server_url (the base URL of the OAuth server, excluding the /mcp endpoint). MCPO will automatically discover endpoints via /.well-known/oauth-authorization-server and handle registration, authorization, and token refresh.

    Supported server type: streamable-http.

    {
      "mcpServers": {
        "my-oauth-server": {
          "type": "streamable-http",
          "url": "http://localhost:8000/mcp",
          "oauth": {
            "server_url": "http://localhost:8000"
          }
        }
      }
    }
  4. Proxy SSE and Streamable HTTP MCP servers

    main

    mcpo can proxy MCP servers that communicate via SSE or Streamable HTTP instead of standard stdio.

    SSE-compatible MCP server: Use --server-type "sse" and provide the endpoint. You can also pass custom headers using --header.

    mcpo --port 8000 --api-key "top-secret" --server-type "sse" -- http://127.0.0.1:8001/sse

    Streamable HTTP-compatible MCP server: Use --server-type "streamable-http" and provide the endpoint.

    mcpo --port 8000 --api-key "top-secret" --server-type "streamable-http" -- http://127.0.0.1:8002/mcp
    mcpo --port 8000 --api-key "top-secret" --server-type "sse" -- http://127.0.0.1:8001/sse
  5. Configure Static Client Metadata for Legacy OAuth Servers

    main

    If your OAuth server does not support dynamic client registration, you must provide client_metadata manually. This allows you to specify the client name, redirect URIs, and grant types required for the connection.

    Note: Do NOT manually set scope, authorization_endpoint, or token_endpoint; these are discovered automatically from the server's metadata.

    {
      "mcpServers": {
        "legacy-oauth-server": {
          "type": "streamable-http",
          "url": "http://localhost:8000/mcp", 
          "oauth": {
            "server_url": "http://localhost:8000",
            "storage_type": "file",
            "client_metadata": {
              "client_name": "My MCPO Client",
              "redirect_uris": ["http://localhost:3030/callback"],
              "grant_types": ["authorization_code", "refresh_token"],
              "response_types": ["code"]
            }
          }
        }
      }
    }
  6. Install and run mcpo

    main

    mcpo can be installed and run using uvx (recommended), pip, or Docker. It acts as a proxy that converts MCP server commands into OpenAPI-compatible HTTP servers.

    Using uv (Recommended):

    uvx mcpo --port 8000 --api-key "top-secret" -- your_mcp_server_command

    Using pip:

    pip install mcpo
    mcpo --port 8000 --api-key "top-secret" -- your_mcp_server_command

    Using Docker:

    docker run -p 8000:8000 ghcr.io/open-webui/mcpo:main --api-key "top-secret" -- your_mcp_server_command
    uvx mcpo --port 8000 --api-key "top-secret" -- your_mcp_server_command
  7. Configure OAuth 2.1 Authentication

    main

    mcpo supports OAuth 2.1 for streamable-http server types using dynamic client registration.

    Basic Configuration:

    {
      "mcpServers": {
        "oauth-protected-server": {
          "type": "streamable-http",
          "url": "http://localhost:8000/mcp",
          "oauth": {
            "server_url": "http://localhost:8000"
          }
        }
      }
    }

    OAuth Configuration Options:

    • server_url (required): OAuth server base URL.
    • storage_type: "file" (persistent, default) or "memory" (session-only).
    • callback_port: Local port for OAuth callback (default: 3030).
    • use_loopback: Whether to auto-open the browser for auth (default: true).

    Advanced (Static Client Metadata): For servers not supporting dynamic registration:

    {
      "mcpServers": {
        "legacy-oauth-server": {
          "type": "streamable-http", 
          "url": "http://api.example.com/mcp",
          "oauth": {
            "server_url": "http://api.example.com",
            "client_metadata": {
              "client_name": "My MCPO Client",
              "redirect_uris": ["http://localhost:3030/callback"]
            }
          }
        }
      }
    }

    Note: Do not manually set scope, authorization_endpoint, or token_endpoint; these are discovered automatically.

    {
      "mcpServers": {
        "oauth-protected-server": {
          "type": "streamable-http",
          "url": "http://localhost:8000/mcp",
          "oauth": {
            "server_url": "http://localhost:8000"
          }
        }
      }
    }
  8. Serve mcpo under a subpath using --root-path

    main

    To serve mcpo behind a reverse proxy or under a specific URL prefix (e.g., /api/mcpo), use the --root-path argument. All routes will be prefixed with this path.

    Example:

    mcpo --port 8000 --root-path "/api/mcpo" --api-key "top-secret" -- your_mcp_server_command

    Routes will be accessible at http://localhost:8000/api/mcpo/<tool_name>.

    mcpo --port 8000 --root-path "/api/mcpo" --api-key "top-secret" -- your_mcp_server_command
  9. Configure multiple MCP tools via a config file

    main

    You can serve multiple MCP tools using a single JSON configuration file following the Claude Desktop format. Use the --config flag to specify the file path. Use --hot-reload to automatically reload servers when the config file changes.

    Example config.json:

    {
      "mcpServers": {
        "memory": {
          "command": "npx",
          "args": ["-y", "@modelcontextprotocol/server-memory"]
        },
        "time": {
          "command": "uvx",
          "args": ["mcp-server-time", "--local-timezone=America/New_York"],
          "disabledTools": ["convert_time"]
        },
        "mcp_sse": {
          "type": "sse",
          "url": "http://127.0.0.1:8001/sse",
          "headers": {
            "Authorization": "Bearer token",
            "X-Custom-Header": "value"
          }
        },
        "mcp_streamable_http": {
          "type": "streamable-http",
          "url": "http://127.0.0.1:8002/mcp"
        }
      }
    }

    Run command:

    mcpo --config /path/to/config.json --hot-reload

    Each tool is accessible at http://localhost:8000/<tool_name> and its interactive docs at http://localhost:8000/<tool_name>/docs.

    mcpo --config /path/to/config.json --hot-reload
  10. Implement OAuth callback flows

    main

    The create_oauth_provider function supports two modes of handling the OAuth callback:

    • Loopback Mode (use_loopback=True): Starts a local CallbackServer on the specified callback_port. It automatically opens the user's browser to the authorization URL and waits for the redirect to http://localhost:{port}/callback.
    • Manual Mode (use_loopback=False): Prints the authorization URL to the console and prompts the user to paste the full callback URL once they have authorized the application.
  11. Troubleshoot OAuth Issues in MCPO

    main

    Common issues and solutions:

    • "OAuth server_url required": Ensure server_url is present in the oauth section of your config.
    • Browser doesn't open: Verify use_loopback is true and a default browser is configured on your system.
    • "No authorization code found": If using manual flow, ensure you copy the complete callback URL including all query parameters.
    • Port already in use: Change the callback_port to a different number.
    • Tokens not persisting: Verify storage_type is set to "file" and MCPO has write permissions to ~/.mcpo/tokens/.
  12. Configure environment variables in mcpo

    main

    You can provide environment variables to the proxied MCP server using two methods:

    1. Inline via --env: Pass multiple KEY=VALUE pairs.
    2. Via a file using --env-path: Specify a path to a .env file. The CLI will load these variables using load_dotenv and merge them into the environment.

    Note: If log_level is provided via CLI, it is also injected into the environment as LOG_LEVEL.