introspect-schema
Retrieve GraphQL schema details by introspecting the endpoint, enabling users to access schema information before executing queries. Supports custom headers and endpoint overrides.
Instructions
Introspect the GraphQL schema, use this tool before doing a query to get the schema information if you do not have it available as a resource already.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| endpoint | No | Optional: Override the default endpoint, the already used endpoint is: http://localhost:3000/graphql | |
| headers | No | Optional: Add additional headers, the already used headers are: {} |
Implementation Reference
- src/index.ts:86-114 (handler)The inline handler function for the 'introspect-schema' tool. It determines the schema source based on environment variables and calls the appropriate introspection helper, returning the schema as text content or an error.async () => { try { let schema: string; if (env.SCHEMA) { schema = await introspectLocalSchema(env.SCHEMA); } else { schema = await introspectEndpoint(env.ENDPOINT, env.HEADERS); } return { content: [ { type: "text", text: schema, }, ], }; } catch (error) { return { isError: true, content: [ { type: "text", text: `Failed to introspect schema: ${error}`, }, ], }; } },
- src/index.ts:79-85 (schema)Zod input schema for the tool, featuring a dummy '__ignore__' boolean parameter to accommodate clients sending undefined arguments.// This is a workaround to help clients that can't handle an empty object as an argument // They will often send undefined instead of an empty object which is not allowed by the schema __ignore__: z .boolean() .default(false) .describe("This does not do anything"), },
- src/index.ts:75-115 (registration)Registers the 'introspect-schema' tool with the MCP server, providing name, description, input schema, and handler function.server.tool( "introspect-schema", "Introspect the GraphQL schema, use this tool before doing a query to get the schema information if you do not have it available as a resource already.", { // This is a workaround to help clients that can't handle an empty object as an argument // They will often send undefined instead of an empty object which is not allowed by the schema __ignore__: z .boolean() .default(false) .describe("This does not do anything"), }, async () => { try { let schema: string; if (env.SCHEMA) { schema = await introspectLocalSchema(env.SCHEMA); } else { schema = await introspectEndpoint(env.ENDPOINT, env.HEADERS); } return { content: [ { type: "text", text: schema, }, ], }; } catch (error) { return { isError: true, content: [ { type: "text", text: `Failed to introspect schema: ${error}`, }, ], }; } }, );
- src/helpers/introspection.ts:10-35 (helper)'introspectEndpoint' helper function that sends an introspection query to a GraphQL endpoint and returns the schema in SDL format using GraphQL utilities.export async function introspectEndpoint( endpoint: string, headers?: Record<string, string>, ) { const response = await fetch(endpoint, { method: "POST", headers: { "Content-Type": "application/json", ...headers, }, body: JSON.stringify({ query: getIntrospectionQuery(), }), }); if (!response.ok) { throw new Error(`GraphQL request failed: ${response.statusText}`); } const responseJson = await response.json(); // Transform to a schema object const schema = buildClientSchema(responseJson.data); // Print the schema SDL return printSchema(schema); }
- src/helpers/introspection.ts:58-61 (helper)'introspectLocalSchema' helper function that reads a local GraphQL schema file and returns its contents.export async function introspectLocalSchema(path: string) { const schema = await readFile(path, "utf8"); return schema; }