Skip to main content

MCP server for GraphQL

Project description

MCP GraphQL

An MCP (Model Context Protocol) server that enables interaction with GraphQL APIs.

Description

MCP GraphQL is a tool that implements the Model Context Protocol (MCP) to provide a standardized interface for interacting with GraphQL APIs. It automatically exposes each GraphQL query as a separate MCP tool, allowing MCP-compatible clients to seamlessly communicate with GraphQL services.

Features

  • Each GraphQL query is exposed as a distinct MCP tool
  • Tool parameters automatically match the corresponding GraphQL query parameters
  • JSON schema for tool inputs is dynamically generated from GraphQL query parameters
  • No schema definition required - simply provide the API URL and credentials
  • Currently supports GraphQL queries (mutations support planned for future releases)
  • Configurable authentication (Bearer, Basic, custom headers)
  • Automatic handling of complex GraphQL types

Requirements

  • Python 3.11 or higher

Installation

Using uv (recommended)

When using uv no specific installation is needed. We will use uvx to directly run mcp-graphql.

Using pip

Alternatively you can install mcp-graphql via pip:

pip install mcp-graphql

Installation from source code

git clone https://github.com/your-username/mcp_graphql.git
cd mcp_graphql
pip install .

Usage

As a command line tool

Using uvx:

uvx mcp-graphql --api-url="https://api.example.com/graphql" --auth-token="your-token"

Using pip installation:

mcp-graphql --api-url="https://api.example.com/graphql" --auth-token="your-token"

or

python -m mcp_graphql --api-url="https://api.example.com/graphql" --auth-token="your-token"

Available options

  • --api-url: GraphQL API URL (required)
  • --auth-token: Authentication token (optional, can also be set via MCP_AUTH_TOKEN environment variable)
  • --auth-type: Authentication type, default is "Bearer" (optional)
  • --auth-headers: Custom authentication headers in JSON format (optional)
  • --queries-file: Path to a .gql file containing predefined GraphQL queries (optional)
  • --queries: Predefined GraphQL queries passed directly as a string (optional)
  • --max-depth: Maximum depth when auto-generating queries (default: 5)

Example with custom headers:

mcp-graphql --api-url="https://api.example.com/graphql" --auth-headers='{"Authorization": "Bearer token", "X-API-Key": "key"}'

Example with predefined queries file:

mcp-graphql --api-url="https://api.example.com/graphql" --queries-file="./queries.gql"

Example passing queries directly as a string (use single quotes to avoid shell conflicts):

mcp-graphql --api-url="https://api.example.com/graphql" --queries='query Hello { hello }'

About automatic query generation

If neither --queries-file nor --queries is supplied, mcp-graphql will automatically build a query by introspecting the GraphQL schema and selecting all scalar fields up to a configurable depth. This is convenient for quickly exploring an API, but it has two main drawbacks:

  1. Too much depth – drilling deep into nested objects (especially lists) can return a large amount of data and overflow the LLM context window.
  2. Lack of control – you cannot precisely choose which fields are included, so tokens may be wasted on irrelevant information.

The --max-depth option mitigates the first issue by limiting the recursion depth (default = 5). Even so, the best practice is to define the exact queries you need through --queries-file or --queries. In doing so:

  • You control exactly which fields are returned and avoid unnecessary lists.
  • Every named operation in your file/string is automatically exposed as an MCP tool with no manual boilerplate.

Example using --max-depth to limit the auto-generated query to depth 2:

mcp-graphql --api-url="https://api.example.com/graphql" --max-depth 2

For production workloads you should supply your own queries:

# Using a file
mcp-graphql --api-url="https://api.example.com/graphql" \
            --queries-file="./queries.gql"

# Or as a string
mcp-graphql --api-url="https://api.example.com/graphql" \
            --queries='query UserMini { viewer { id name } }'

The queries.gql file should contain one or more named operations, e.g.:

# queries.gql
query GetUser($id: ID!) {
  user(id: $id) {
    id
    name
    email
  }
}

query ListPosts {
  posts {
    id
    title
  }
}

As a library

import asyncio
from pathlib import Path
from mcp_graphql import make_server, serve

auth_headers = {"Authorization": "Bearer your-token"}
api_url = "https://api.example.com/graphql"
queries_file = Path("queries.gql")  # optional, set to None to expose all queries

asyncio.run(serve(api_url, auth_headers, queries_file=queries_file))

Passing the queries directly as a string from code:

queries_str = """
query Hello($name: String!) {
  hello(name: $name)
}
"""

asyncio.run(serve(api_url, auth_headers, queries=queries_str, max_depth=3))

Create low level MCP server:

server = make_server(api_url, auth_headers, queries=queries_str, max_depth=3)

For low level server with Streamable HTTP implementations, see:

Configuration

Configure for Claude.app

Add to your Claude settings:

Using uvx
"mcpServers": {
  "graphql": {
    "command": "uvx",
    "args": ["mcp-graphql", "--api-url", "https://api.example.com/graphql"]
  }
}
Using docker
"mcpServers": {
  "graphql": {
    "command": "docker",
    "args": ["run", "-i", "--rm", "mcp/graphql", "--api-url", "https://api.example.com/graphql"]
  }
}
Using pip installation
"mcpServers": {
  "graphql": {
    "command": "python",
    "args": ["-m", "mcp_graphql", "--api-url", "https://api.example.com/graphql"]
  }
}

How It Works

MCP GraphQL automatically:

  1. Introspects the provided GraphQL API
  2. Creates an MCP tool for each available GraphQL query
  3. Generates JSON schema for tool inputs based on query parameters
  4. Handles type conversions between GraphQL and JSON

When a tool is called, the server:

  1. Converts the tool call parameters to a GraphQL query
  2. Executes the query against the API
  3. Returns the results to the MCP client

Planned Features

  • Support for GraphQL mutations (with appropriate safeguards)
  • Improved error handling and validation
  • Additional optimizations based on specific GraphQL API implementations

Development

Setting up the development environment

# Create virtual environment using uv
uv venv

# Install dependencies
uv sync

Linting

ruff check .

Running the server in development mode

When working locally you can start the MCP GraphQL server with hot-reloading and inspect its tools using the Model Context Protocol Inspector:

npx "@modelcontextprotocol/inspector" uv run -n --project $PWD mcp-graphql --api-url http://localhost:3010/graphql

Replace http://localhost:3010/graphql with the URL of your local GraphQL endpoint if it differs.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Contributing

Contributions are welcome. Please feel free to submit a Pull Request or open an Issue.

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

iflow_mcp_drestrepom_mcp_graphql-0.4.1.tar.gz (10.7 kB view details)

Uploaded Source

Built Distribution

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

File details

Details for the file iflow_mcp_drestrepom_mcp_graphql-0.4.1.tar.gz.

File metadata

  • Download URL: iflow_mcp_drestrepom_mcp_graphql-0.4.1.tar.gz
  • Upload date:
  • Size: 10.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.0 {"installer":{"name":"uv","version":"0.10.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for iflow_mcp_drestrepom_mcp_graphql-0.4.1.tar.gz
Algorithm Hash digest
SHA256 1215dfd43eb3ff205a1776d9da5ae7070cc8792b67d5a333e1192109f8339452
MD5 317d30e9667e6922046267bfe2af62d5
BLAKE2b-256 b1c536fbe69c5b1f6552c5781a105bf620df9de70ff6eca77cac6a0ff62e581c

See more details on using hashes here.

File details

Details for the file iflow_mcp_drestrepom_mcp_graphql-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: iflow_mcp_drestrepom_mcp_graphql-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 12.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.0 {"installer":{"name":"uv","version":"0.10.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for iflow_mcp_drestrepom_mcp_graphql-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e3b49c805db813ed94e4693800b43183aaa8552540d966a99e2beaa82abf73ce
MD5 d915305b31f01c9860e61d2977ef20d6
BLAKE2b-256 4ec0a9b2f053311cf3f981e1148dce0153b3046431f4ba5b78e10cf96231cb94

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