introspectGraphQLSchema
Fetch the schema of a GraphQL API using introspection to understand its structure and available operations. Returns the schema in JSON format for analysis and integration.
Instructions
Fetches the schema of the target GraphQL API using introspection. Returns the schema in JSON format.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/tools/introspection.ts:22-75 (handler)Handler function that performs GraphQL schema introspection by executing the standard introspection query and handling responses and errors.export async function handleIntrospection(): Promise<IntrospectionResult> { const introspectionQuery = getIntrospectionQuery(); // Get the standard query try { console.error("Executing GraphQL introspection query..."); const response = await executeGraphQL<IntrospectionResult>( introspectionQuery ); // Check for GraphQL errors returned in the response body if (response.errors && response.errors.length > 0) { const errorMessages = response.errors.map((e) => e.message).join("; "); console.error( `GraphQL introspection query returned errors: ${errorMessages}`, response.errors ); // Using InternalError as a placeholder. throw new McpError( ErrorCode.InternalError, `Introspection query failed: ${errorMessages}`, { details: response.errors } // Include original errors in details ); } if (!response.data || !response.data.__schema) { console.error( "Introspection query did not return a valid __schema object.", response.data ); // TODO: Revisit ErrorCode mapping once SDK types are clarified. // Using InternalError as a placeholder. throw new McpError( ErrorCode.InternalError, "Introspection query did not return a valid schema." ); } console.error("GraphQL introspection query successful."); return response.data; // Return the { data: { __schema: ... } } part } catch (error) { // Catch errors thrown by executeGraphQL (network, HTTP errors) or our own McpErrors console.error("Error executing introspection query:", error); if (error instanceof McpError) { // Re-throw McpErrors directly throw error; } // Wrap unexpected errors (using PascalCase) throw new McpError( ErrorCode.InternalError, "An unexpected error occurred during schema introspection.", { cause: error as Error } ); } }
- src/tools/introspection.ts:6-7 (schema)Zod schema defining the input for the introspectGraphQLSchema tool, which requires no parameters.// Define the Zod schema for the input (no parameters needed) export const IntrospectSchemaInput = z.object({});
- src/server.ts:46-51 (registration)Tool registration in the ListTools response, including name, description, and input schema reference.{ name: "introspectGraphQLSchema", description: "Fetches the schema of the target GraphQL API using introspection. Returns the schema in JSON format.", inputSchema: introspectionInputSchema, },
- src/server.ts:80-85 (registration)Dispatcher in CallTool handler that validates input and invokes the introspection handler.case "introspectGraphQLSchema": // Validate arguments using the Zod schema IntrospectSchemaInput.parse(args); console.error(`Calling handler for ${name}`); result = await handleIntrospection(); break;