Skip to main content
Glama
letoribo

mcp-graphql-enhanced

query-graphql

Execute GraphQL queries with custom variables and headers to interact with GraphQL endpoints directly from the MCP server.

Instructions

Query a GraphQL endpoint with the given query and variables. Optionally pass headers (e.g., for Authorization).

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
queryYes
variablesNo
headersNoOptional JSON string of headers to include, e.g., {"Authorization": "Bearer ..."}

Implementation Reference

  • The main handler function that parses the GraphQL query, checks for mutations, merges headers, executes the fetch request to the endpoint, handles errors, and returns formatted results or errors.
    const queryGraphqlHandler = async ({ query, variables, headers }: any) => {
    	try {
    		const parsedQuery = parse(query);
    
    		const isMutation = parsedQuery.definitions.some(
    			(def: any) =>
    				def.kind === "OperationDefinition" && def.operation === "mutation",
    		);
    
    		if (isMutation && !env.ALLOW_MUTATIONS) {
    			return {
    				isError: true,
    				content: [
    					{
    						type: "text",
    						text: "Mutations are not allowed unless you enable them in the configuration. Please use a query operation instead.",
    					},
    				],
    			};
    		}
    	} catch (error) {
    		return {
    			isError: true,
    			content: [
    				{
    					type: "text",
    					text: `Invalid GraphQL query: ${error}`,
    				},
    			],
    		};
    	}
    
    	try {
    		const toolHeaders = headers
    			? JSON.parse(headers)
    			: {};
    
    		const allHeaders = {
    			"Content-Type": "application/json",
    			...env.HEADERS,
    			...toolHeaders,
    		};
    
    		let parsedVariables = null;
    		if (variables) {
    			if (typeof variables === 'string') {
    				parsedVariables = JSON.parse(variables);
    			} else {
    				parsedVariables = variables;
    			}
    		}
    
    		const response = await fetch(env.ENDPOINT, {
    			method: "POST",
    			headers: allHeaders,
    			body: JSON.stringify({
    				query,
    				variables: parsedVariables,
    			}),
    		});
    
    		if (!response.ok) {
    			const responseText = await response.text();
    			return {
    				isError: true,
    				content: [
    					{
    						type: "text",
    						text: `GraphQL request failed: ${response.statusText}\n${responseText}`,
    					},
    				],
    			};
    		}
    
    		const rawData = await response.json();
    
    		const data = rawData;
    
    		if (data.errors && data.errors.length > 0) {
    			return {
    				isError: true,
    				content: [
    					{
    						type: "text",
    						text: `GraphQL errors: ${JSON.stringify(data.errors, null, 2)}`,
    					},
    				],
    			};
    		}
    
    		return {
    			content: [
    				{
    					type: "text",
    					text: JSON.stringify(data, null, 2),
    				},
    			],
    		};
    	} catch (error) {
    		return {
    			isError: true,
    			content: [
    				{
    					type: "text",
    					text: `Failed to execute GraphQL query: ${error}`,
    				},
    			],
    		};
    	}
    };
  • src/index.ts:247-259 (registration)
    Registers the 'query-graphql' tool with the MCP server, providing the tool name, description, input schema using Zod, and references the handler function.
    server.tool(
    	"query-graphql",
    	"Query a GraphQL endpoint with the given query and variables. Optionally pass headers (e.g., for Authorization).",
    	{
    		query: z.string(),
    		variables: z.string().optional(),
    		headers: z
    			.string()
    			.optional()
    			.describe("Optional JSON string of headers to include, e.g., {\"Authorization\": \"Bearer ...\"}"),
    	},
    	queryGraphqlHandler
    );
  • Zod schema defining the input parameters for the 'query-graphql' tool: required 'query' string, optional 'variables' string, optional 'headers' string.
    {
    	query: z.string(),
    	variables: z.string().optional(),
    	headers: z
    		.string()
    		.optional()
    		.describe("Optional JSON string of headers to include, e.g., {\"Authorization\": \"Bearer ...\"}"),
    },
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries the full burden. It mentions optional headers for Authorization but lacks details on error handling, rate limits, authentication requirements, or response format. For a GraphQL query tool with zero annotation coverage, this leaves significant behavioral gaps.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is front-loaded and concise, consisting of two efficient sentences that directly state the tool's function and an optional feature. Every sentence adds value without redundancy.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the complexity of a GraphQL query tool with no annotations, no output schema, and incomplete parameter documentation (33% schema coverage), the description is inadequate. It misses critical details like response structure, error cases, and full parameter explanations, making it insufficient for effective agent use.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 33% (only 'headers' has a description). The description adds minimal value by noting headers are 'optional' and for 'Authorization,' but doesn't explain 'query' or 'variables' parameters beyond what's implied by the tool name. With low schema coverage, the description doesn't fully compensate for undocumented parameters.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose: 'Query a GraphQL endpoint with the given query and variables.' It specifies the verb ('query') and resource ('GraphQL endpoint'), though it doesn't explicitly differentiate from the sibling 'introspect-schema' tool, which likely serves a different purpose (schema introspection vs. general querying).

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines3/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides implied usage guidance by mentioning headers 'e.g., for Authorization,' suggesting authentication contexts. However, it doesn't explicitly state when to use this tool versus the sibling 'introspect-schema' or other alternatives, nor does it outline prerequisites or exclusions.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/letoribo/mcp-graphql-enhanced'

If you have feedback or need assistance with the MCP directory API, please join our Discord server