execute_graphql
Execute arbitrary GraphQL queries and mutations against the API when no specific tool exists.
Instructions
Execute any GraphQL query or mutation against the API. Use this when no specific tool exists for your operation.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | Full GraphQL query or mutation string including selection set | |
| variables | No | Variables for the operation |
Implementation Reference
- src/index.ts:299-307 (handler)The handler function for the execute_graphql tool. It receives a query string and optional variables, executes the GraphQL request via graphql-request client, and returns the result as formatted JSON text. Errors are caught and returned as error content.
async ({ query, variables }) => { try { const data = await client.request(query, variables ?? {}); return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] }; } catch (err) { const msg = err instanceof Error ? err.message : String(err); return { content: [{ type: "text", text: `Error: ${msg}` }], isError: true }; } } - src/index.ts:295-298 (schema)The input schema for execute_graphql. Defines two parameters: 'query' (required string) and 'variables' (optional record of unknown values) for the GraphQL operation.
{ query: z.string().describe("Full GraphQL query or mutation string including selection set"), variables: z.record(z.unknown()).optional().describe("Variables for the operation"), }, - src/index.ts:292-308 (registration)Registration of the execute_graphql tool on the MCP server using server.tool(). It's registered as a generic fallback tool, always available regardless of schema introspection success.
server.tool( "execute_graphql", "Execute any GraphQL query or mutation against the API. Use this when no specific tool exists for your operation.", { query: z.string().describe("Full GraphQL query or mutation string including selection set"), variables: z.record(z.unknown()).optional().describe("Variables for the operation"), }, async ({ query, variables }) => { try { const data = await client.request(query, variables ?? {}); return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] }; } catch (err) { const msg = err instanceof Error ? err.message : String(err); return { content: [{ type: "text", text: `Error: ${msg}` }], isError: true }; } } ); - src/index.ts:24-24 (helper)The GraphQL client used by the execute_graphql handler to make requests.
const client = new GraphQLClient(GRAPHQL_URL, { headers });