Skip to main content
Glama

query

Execute GraphQL queries with specified variables using the MCP server, enabling AI models to interact with GraphQL APIs for data retrieval and processing.

Instructions

Query a GraphQL endpoint with the given query and variables

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
queryYes
variablesNo

Implementation Reference

  • The handler function for the 'query' tool. It validates the GraphQL query (parses it and checks for mutations if not allowed), sends a POST request to the GraphQL endpoint with the query and optional variables, handles various errors (invalid query, HTTP errors, GraphQL errors), and returns the JSON response or error messages.
    async ({ query, variables }) => { try { const parsedQuery = parse(query); // Check if the query is a mutation const isMutation = parsedQuery.definitions.some( (def) => 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 response = await fetch(env.ENDPOINT, { method: "POST", headers: { "Content-Type": "application/json", ...env.HEADERS, }, body: JSON.stringify({ query, variables, }), }); if (!response.ok) { const responseText = await response.text(); return { isError: true, content: [ { type: "text", text: `GraphQL request failed: ${response.statusText}\n${responseText}`, }, ], }; } const data = (await response.json()) as any; if (data.errors && data.errors.length > 0) { // Contains GraphQL errors return { isError: true, content: [ { type: "text", text: `The GraphQL response has errors, please fix the query: ${JSON.stringify( data, null, 2 )}`, }, ], }; } return { content: [ { type: "text", text: JSON.stringify(data, null, 2), }, ], }; } catch (error) { throw new Error(`Failed to execute GraphQL query: ${error}`); } }
  • The input schema for the 'query' tool using Zod, requiring a 'query' string and optionally accepting 'variables' as a string.
    { query: z.string(), variables: z.string().optional(), },
  • src/index.ts:115-213 (registration)
    The registration of the 'query' tool on the MCP server using server.tool(), including name, description, schema, and handler.
    server.tool( "query", "Query a GraphQL endpoint with the given query and variables", { query: z.string(), variables: z.string().optional(), }, async ({ query, variables }) => { try { const parsedQuery = parse(query); // Check if the query is a mutation const isMutation = parsedQuery.definitions.some( (def) => 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 response = await fetch(env.ENDPOINT, { method: "POST", headers: { "Content-Type": "application/json", ...env.HEADERS, }, body: JSON.stringify({ query, variables, }), }); if (!response.ok) { const responseText = await response.text(); return { isError: true, content: [ { type: "text", text: `GraphQL request failed: ${response.statusText}\n${responseText}`, }, ], }; } const data = (await response.json()) as any; if (data.errors && data.errors.length > 0) { // Contains GraphQL errors return { isError: true, content: [ { type: "text", text: `The GraphQL response has errors, please fix the query: ${JSON.stringify( data, null, 2 )}`, }, ], }; } return { content: [ { type: "text", text: JSON.stringify(data, null, 2), }, ], }; } catch (error) { throw new Error(`Failed to execute GraphQL query: ${error}`); } } );

Other Tools

Related 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/FabrWill/gql-mcp'

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