wgc
Version:
The official CLI tool to manage the GraphQL Federation Platform Cosmo
64 lines • 2.69 kB
JavaScript
import { z } from 'zod';
import { parse, validate } from 'graphql';
import { buildSchemaWithoutDirectives } from './utils/schema.js';
/**
* Registers the verify-query-against-in-memory-schema tool with the MCP server.
*
* @param params - The parameters for registration.
* @param params.server - The MCP server instance.
* @param params.opts - Base command options.
*/
export const registerVerifyQueryAgainstInMemorySchemaTool = ({ server, opts, }) => {
server.registerTool('verify_query_against_in_memory_schema', {
title: 'Verify Query Against In Memory Schema',
description: 'Verify if a GraphQL query is valid against a local in memory Supergraph or GraphQL SDL.',
inputSchema: { query: z.string(), schema: z.string() },
}, ({ query, schema: schemaString }) => {
try {
let document;
try {
document = parse(query);
}
catch (syntaxError) {
return {
content: [{ type: 'text', text: `Query parsing failed:\n${syntaxError.message}` }],
};
}
// Build the schema from the string, removing all directives
let schema;
try {
schema = buildSchemaWithoutDirectives(schemaString);
}
catch (schemaError) {
return {
content: [{ type: 'text', text: schemaError.message }],
};
}
// Validate the query against the schema
const validationErrors = validate(schema, document);
if (validationErrors.length > 0) {
const errorMessages = validationErrors
.map((error) => {
var _a;
const locations = ((_a = error.locations) === null || _a === void 0 ? void 0 : _a.map((loc) => `line ${loc.line}, column ${loc.column}`).join(', ')) ||
'unknown location';
return `- ${error.message} (at ${locations})`;
})
.join('\n');
return {
content: [{ type: 'text', text: `Query validation failed:\n${errorMessages}` }],
};
}
return {
content: [{ type: 'text', text: 'Query is valid against the schema.' }],
};
}
catch (error) {
// Handle other unexpected errors
return {
content: [{ type: 'text', text: `An unexpected error occurred: ${error.message}` }],
};
}
});
};
//# sourceMappingURL=verify-query-against-in-memory-schema.js.map