wgc
Version:
The official CLI tool to manage the GraphQL Federation Platform Cosmo
52 lines • 2.33 kB
JavaScript
import { z } from 'zod';
import { introspectSubgraph } from '../../../utils.js';
/**
* Zod schema for the introspect subgraph tool input.
* @property routingUrl - The routing URL of the subgraph.
* @property header - Optional headers for introspection.
* @property useRawIntrospection - Optional flag to use raw introspection query.
*/
export const introspectSubgraphInputSchema = z.object({
routingUrl: z.string().describe('The routing url of your subgraph.'),
header: z
.array(z.object({ key: z.string(), value: z.string() }))
.optional()
.describe('Headers to apply during introspection'),
useRawIntrospection: z.boolean().optional().describe('Use the standard introspection query.'),
});
/**
* Registers the introspect subgraph tool with the MCP server.
*
* @param config - Configuration object containing the MCP server and base command options.
* @param config.server - The MCP server instance.
* @param config.opts - Base command options.
*/
export const registerIntrospectSubgraphTool = ({ server, opts }) => {
server.registerTool('introspect_subgraph', {
title: 'Introspect Subgraph',
description: 'Introspects a subgraph and returns its GraphQL schema (SDL).',
inputSchema: introspectSubgraphInputSchema.shape,
}, async ({ routingUrl, header, useRawIntrospection }) => {
// Destructure input fields directly
try {
const resp = await introspectSubgraph({
subgraphURL: routingUrl, // Use destructured variable
additionalHeaders: header || [],
rawIntrospection: useRawIntrospection, // Use destructured variable
});
if (resp.success !== true || !resp.sdl) {
// Throw error on failure
throw new Error(`Could not introspect subgraph at ${routingUrl}. ${resp.errorMessage || 'Unknown error'}`); // Use destructured variable
}
// Return result wrapped in content object
return {
content: [{ type: 'text', text: resp.sdl }],
};
}
catch (error) {
// Rethrow caught errors
throw new Error(`Failed to introspect subgraph: ${error.message || error}`);
}
});
};
//# sourceMappingURL=introspect-subgraph.js.map