graphql-mcp
Version:
A Model Context Protocol server that enables LLMs to interact with GraphQL APIs through dynamic schema introspection and query execution
339 lines (271 loc) • 6.4 kB
Markdown
This document provides step-by-step examples of how to use the GraphQL MCP Server.
```bash
npm install graphql-mcp
npm start
```
Let's start with a public GraphQL API that doesn't require authentication:
```json
{
"tool": "graphql_configure",
"arguments": {
"endpoint": "https://countries.trevorblades.com/",
"timeout": 30000,
"maxDepth": 5,
"maxComplexity": 50
}
}
```
Discover what data is available:
```json
{
"tool": "graphql_introspect",
"arguments": {
"includeDeprecated": false,
"includeDescriptions": true
}
}
```
This will return the complete schema structure, showing all available types, fields, and operations.
```json
{
"tool": "graphql_query",
"arguments": {
"query": "query { countries { name code emoji } }"
}
}
```
```json
{
"tool": "graphql_query",
"arguments": {
"query": "query GetCountry($code: ID!) { country(code: $code) { name capital currency phone languages { name } } }",
"variables": {
"code": "US"
}
}
}
```
```json
{
"tool": "graphql_query",
"arguments": {
"query": "query GetCountries($filter: CountryFilterInput) { countries(filter: $filter) { name code continent { name } } }",
"variables": {
"filter": {
"continent": {
"eq": "NA"
}
}
}
}
}
```
```json
{
"tool": "graphql_configure",
"arguments": {
"endpoint": "https://api.example.com/graphql",
"maxDepth": 3,
"maxComplexity": 20,
"timeout": 10000
}
}
```
```json
{
"tool": "graphql_configure",
"arguments": {
"endpoint": "https://api.example.com/graphql",
"disabledResolvers": [
"sensitiveUserData",
"adminOnlyField",
"expensiveComputation"
]
}
}
```
```json
{
"tool": "graphql_configure",
"arguments": {
"endpoint": "https://api.example.com/graphql",
"headers": {
"X-API-Key": "your-api-key-here",
"User-Agent": "GraphQL-MCP/1.0.0"
}
}
}
```
```json
{
"tool": "graphql_configure",
"arguments": {
"endpoint": "https://api.github.com/graphql",
"headers": {
"Authorization": "Bearer ghp_your_github_token_here",
"User-Agent": "GraphQL-MCP/1.0.0"
}
}
}
```
```json
{
"tool": "graphql_configure",
"arguments": {
"endpoint": "https://api.example.com/graphql",
"headers": {
"Authorization": "Bearer token123",
"X-Custom-Header": "custom-value",
"Accept": "application/json",
"Content-Type": "application/json"
}
}
}
```
The server automatically handles GraphQL errors and returns them in a structured format:
```json
{
"tool": "graphql_query",
"arguments": {
"query": "query { nonExistentField }"
}
}
```
Response:
```json
{
"errors": [
{
"message": "Cannot query field 'nonExistentField' on type 'Query'",
"extensions": {
"code": "GRAPHQL_ERROR"
}
}
]
}
```
```json
{
"tool": "graphql_configure",
"arguments": {
"endpoint": "not-a-valid-url"
}
}
```
Response:
```json
{
"error": "Configuration failed: GraphQL endpoint must be a valid URL"
}
```
```json
{
"tool": "graphql_status"
}
```
Response:
```json
{
"configured": true,
"endpoint": "https://api.example.com/graphql",
"timeout": 30000,
"maxDepth": 10,
"maxComplexity": 100,
"disabledResolvers": ["sensitiveField"],
"headersCount": 2
}
```
Set the `DEBUG` environment variable:
```bash
DEBUG=true npm start
```
Begin with basic queries and gradually add complexity:
```json
// Start with this
{ "tool": "graphql_query", "arguments": { "query": "query { __typename }" } }
// Then move to this
{ "tool": "graphql_query", "arguments": { "query": "query { users { id name } }" } }
// Finally add variables and fragments
{ "tool": "graphql_query", "arguments": { "query": "query GetUser($id: ID!) { user(id: $id) { ...UserFragment } } fragment UserFragment on User { id name email }", "variables": { "id": "123" } } }
```
Always introspect the schema first to understand the available data:
```json
{ "tool": "graphql_introspect" }
```
Configure security limits based on your use case:
```json
{
"tool": "graphql_configure",
"arguments": {
"endpoint": "https://api.example.com/graphql",
"maxDepth": 5, // Adjust based on your schema depth
"maxComplexity": 100, // Adjust based on your performance needs
"timeout": 30000 // 30 seconds is usually sufficient
}
}
```
Use the `disabledResolvers` feature to prevent access to sensitive fields:
```json
{
"tool": "graphql_configure",
"arguments": {
"endpoint": "https://api.example.com/graphql",
"disabledResolvers": [
"password",
"privateKey",
"internalNotes",
"adminOnlyData"
]
}
}
```
For complex queries, always use operation names:
```json
{
"tool": "graphql_query",
"arguments": {
"query": "query GetUserProfile($userId: ID!) { user(id: $userId) { name email } }",
"variables": { "userId": "123" },
"operationName": "GetUserProfile"
}
}
```
1. **Invalid Endpoint**: Ensure the endpoint URL is correct and accessible
2. **Authentication Errors**: Verify your tokens and API keys are valid
3. **Query Complexity**: Reduce query depth or complexity if hitting limits
4. **Network Timeouts**: Increase the timeout value for slow APIs
- Check the server logs for detailed error messages
- Use the `graphql_status` tool to verify your configuration
- Try simpler queries first to isolate issues
- Consult the API documentation for the GraphQL endpoint you're using