graphql-mcp-server

graphql-mcp-server

Automatically generates MCP tools from any GraphQL API by introspecting its schema, supporting queries, mutations, and authentication.

Category
访问服务器

README

GraphQL MCP Server

A Model Context Protocol (MCP) server that dynamically generates tools for any GraphQL API by introspecting its schema.

Features

  • Automatic tool generation from GraphQL schema introspection
  • Support for all GraphQL queries and mutations
  • Type-safe variable handling
  • Automatic query generation with smart field selection
  • Error handling and validation
  • Function exposure control via exposed.yaml
  • Bearer token authentication support

Function Exposure Control

The server automatically manages which GraphQL functions are exposed as MCP tools through an exposed.yaml file:

How it works

  1. First run: Server discovers all GraphQL queries and mutations, creates exposed.yaml with all functions set to true
  2. Subsequent runs: Only functions marked as true in exposed.yaml are registered as MCP tools
  3. Dynamic updates: New functions are automatically added to the file with default true value
  4. Cleanup: Functions that no longer exist in the schema are removed from the file

exposed.yaml format

exposed:
  queries:
    getUser: true
    listPosts: false
    searchContent: true
  mutations:
    createUser: true
    deleteUser: false
    updatePost: true

Managing exposed functions

  • Set a function to false to disable it (won't be registered as MCP tool)
  • Set a function to true to enable it (will be registered as MCP tool)
  • The file is automatically updated when the schema changes

Installation

npm install

Configuration

GraphQL Endpoint

Set the GraphQL endpoint URL via environment variable or command line argument:

# Via environment variable
export GRAPHQL_URL="https://api.example.com/graphql"

# Via command line argument
node src/index.js --graphql-url "https://api.example.com/graphql"

GraphQL API Authentication

For GraphQL APIs requiring Bearer token authentication:

# Via environment variable
export GRAPHQL_TOKEN="your-bearer-token"

# Via command line argument
node src/index.js --token "your-bearer-token"

# Both URL and token as arguments
node src/index.js --graphql-url "https://api.example.com/graphql" --token "your-bearer-token"

Usage

Running the server

STDIO Transport (default)

npm start
# or
node src/index.js

HTTP Transport

npm run start:http
# or
node src/index.js --transport http

# Custom port
node src/index.js --transport http --port 8080

Development mode

# STDIO transport
npm run dev

# HTTP transport
npm run dev:http

Command Line Options

node src/index.js [options]

Options:
  -t, --transport <type>     Transport type: stdio or http (default: stdio)
  -p, --port <number>        HTTP port (default: 3000)
  -u, --graphql-url <url>    GraphQL endpoint URL
  -T, --token <token>        Bearer token for authentication
  -q, --query-prefix <str>   Prefix for query tools (default: none)
  -m, --mutation-prefix <str> Prefix for mutation tools (default: none)
  -h, --help                 Show help message

Integration with Claude Desktop

STDIO Transport

Add to your Claude Desktop configuration:

{
  "mcpServers": {
    "graphql": {
      "command": "node",
      "args": ["/path/to/graphql-mcp-server/src/index.js"],
      "env": {
        "GRAPHQL_URL": "https://api.example.com/graphql",
        "GRAPHQL_TOKEN": "your-bearer-token"
      }
    }
  }
}

Alternative using command line arguments:

{
  "mcpServers": {
    "graphql": {
      "command": "node",
      "args": [
        "/path/to/graphql-mcp-server/src/index.js",
        "--graphql-url", "https://api.example.com/graphql",
        "--token", "your-bearer-token"
      ]
    }
  }
}

HTTP Transport

For HTTP transport, start the server separately:

# Start the server with environment variables
GRAPHQL_URL="https://api.example.com/graphql" GRAPHQL_TOKEN="your-bearer-token" node src/index.js --transport http --port 3000

# Or using command line arguments
node src/index.js --transport http --port 3000 --graphql-url "https://api.example.com/graphql" --token "your-bearer-token"

Then configure your MCP client to connect to the HTTP endpoint at http://localhost:3000/mcp.

MCP Server Authentication (HTTP Transport)

When using HTTP transport, you can secure the MCP server with bearer token authentication:

# Set AUTH_TOKEN environment variable
export AUTH_TOKEN="your-mcp-secret-token"
node src/index.js --transport http

# Or in docker-compose.yml
environment:
  - AUTH_TOKEN=your-mcp-secret-token

When AUTH_TOKEN is set, clients can authenticate using either:

  1. Authorization header (recommended):

    Authorization: Bearer your-mcp-secret-token
    
  2. URL query parameter:

    http://localhost:3000/mcp?token=your-mcp-secret-token
    

Authentication behavior:

  • If AUTH_TOKEN is set, all requests must provide valid authentication
  • Requests without valid tokens receive 401 Unauthorized
  • If AUTH_TOKEN is not set, the server allows open access (no authentication required)
  • The server checks the Authorization header first, then falls back to the URL parameter

HTTP API Endpoints

When running with HTTP transport:

  • POST /mcp - Main MCP protocol endpoint
  • GET /mcp - Returns method not allowed (405)

Example HTTP Usage

# Without authentication (when AUTH_TOKEN is not set)
curl -X POST http://localhost:3000/mcp \
  -H "Content-Type: application/json" \
  -d '{"jsonrpc": "2.0", "id": 1, "method": "tools/list"}'

# With authentication using header (when AUTH_TOKEN is set)
curl -X POST http://localhost:3000/mcp \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer your-mcp-secret-token" \
  -d '{"jsonrpc": "2.0", "id": 1, "method": "tools/list"}'

# With authentication using URL parameter (when AUTH_TOKEN is set)
curl -X POST "http://localhost:3000/mcp?token=your-mcp-secret-token" \
  -H "Content-Type: application/json" \
  -d '{"jsonrpc": "2.0", "id": 1, "method": "tools/list"}'

# Call a GraphQL query tool
curl -X POST http://localhost:3000/mcp \
  -H "Content-Type: application/json" \
  -d '{
    "jsonrpc": "2.0",
    "id": 1,
    "method": "tools/call",
    "params": {
      "name": "query_getUser",
      "arguments": {
        "variables": {
          "id": "123"
        }
      }
    }
  }'

How it works

  1. On startup, the server connects to the specified GraphQL endpoint (with optional Bearer token authentication)
  2. It loads the exposed.yaml configuration file (creates it if it doesn't exist)
  3. It performs an introspection query to fetch the complete schema
  4. For each query and mutation in the schema, it checks if the function is enabled in exposed.yaml
  5. Only enabled functions are registered as MCP tools with prefixes: query_ for queries and mutation_ for mutations
  6. New functions discovered in the schema are automatically added to exposed.yaml as enabled
  7. When a tool is called, it constructs the appropriate GraphQL query/mutation and executes it
  8. Results are returned as formatted JSON

Tool naming convention

  • Queries: query_<fieldName> (e.g., query_getUser, query_listPosts)
  • Mutations: mutation_<fieldName> (e.g., mutation_createUser, mutation_updatePost)

Environment Variables

Variable Description Required
GRAPHQL_URL GraphQL endpoint URL Yes (if not provided via --graphql-url)
GRAPHQL_TOKEN Bearer token for GraphQL API authentication No
AUTH_TOKEN Bearer token for MCP server authentication (HTTP transport only) No

Files

File Description
exposed.yaml Configuration file controlling which GraphQL functions are exposed as MCP tools

Transport Protocols

The server supports two transport protocols:

STDIO Transport

  • Default transport for MCP
  • Uses standard input/output for communication
  • Suitable for direct integration with MCP clients
  • Lower overhead, faster communication

HTTP Transport

  • StreamableHTTP transport using MCP SDK
  • JSON-RPC over HTTP
  • Express.js server for HTTP handling
  • Suitable for web applications and remote access
  • Standard MCP protocol compliance

Example

For a GraphQL schema with:

type Query {
  getUser(id: ID!): User
  listPosts(limit: Int): [Post]
}

type Mutation {
  createUser(input: CreateUserInput!): User
}

The MCP server will generate tools:

  • query_getUser - with required id parameter
  • query_listPosts - with optional limit parameter
  • mutation_createUser - with required input parameter

License

MIT

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选