synaptra
Version:
A high-performance Model Context Protocol server for GraphQL APIs with advanced features, type-safety, and developer experience improvements
150 lines • 6.19 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleIntrospectSchema = exports.introspectSchemaTool = void 0;
const types_1 = require("../types");
const logger_1 = require("../utils/logger");
exports.introspectSchemaTool = {
name: 'introspect-schema',
description: 'Retrieve and analyze the GraphQL schema from the endpoint. Supports multiple output formats and includes schema statistics.',
inputSchema: {
type: 'object',
properties: {
format: {
type: 'string',
enum: ['sdl', 'json', 'introspection'],
default: 'sdl',
description: 'Output format: sdl (Schema Definition Language), json (parsed schema), or introspection (raw introspection result)',
},
includeDescription: {
type: 'boolean',
default: true,
description: 'Include field and type descriptions in the output',
},
includeDeprecated: {
type: 'boolean',
default: false,
description: 'Include deprecated fields and types',
},
headers: {
type: 'object',
description: 'HTTP headers to include with the request (e.g., Authorization)',
additionalProperties: {
type: 'string'
},
},
},
},
};
const handleIntrospectSchema = async (args, client) => {
const logger = (0, logger_1.getLogger)();
const startTime = Date.now();
try {
// Validate and parse arguments
const params = types_1.IntrospectSchemaParamsSchema.parse(args);
logger.debug('Starting schema introspection', {
format: params.format,
includeDescription: params.includeDescription,
includeDeprecated: params.includeDeprecated,
hasHeaders: !!params.headers,
});
// Get schema info with per-request headers
const schemaInfo = await client.introspectSchema(params.headers);
// Count schema elements
const typeMap = schemaInfo.schema.getTypeMap();
const queryType = schemaInfo.schema.getQueryType();
const mutationType = schemaInfo.schema.getMutationType();
const subscriptionType = schemaInfo.schema.getSubscriptionType();
const types = Object.keys(typeMap).filter(name => !name.startsWith('__')).length;
const queries = queryType ? Object.keys(queryType.getFields()).length : 0;
const mutations = mutationType ? Object.keys(mutationType.getFields()).length : 0;
const subscriptions = subscriptionType ? Object.keys(subscriptionType.getFields()).length : 0;
// Prepare output based on format
let schemaOutput;
switch (params.format) {
case 'json':
schemaOutput = JSON.stringify({
types: typeMap,
queryType: queryType?.name,
mutationType: mutationType?.name,
subscriptionType: subscriptionType?.name,
}, null, 2);
break;
case 'introspection':
// Return raw introspection result
const introspectionQuery = client.getSchema()?.schema;
if (introspectionQuery) {
const { introspectionFromSchema } = await Promise.resolve().then(() => __importStar(require('graphql')));
schemaOutput = JSON.stringify(introspectionFromSchema(introspectionQuery), null, 2);
}
else {
throw new Error('Schema not available for introspection format');
}
break;
case 'sdl':
default:
schemaOutput = schemaInfo.sdl;
break;
}
const duration = Date.now() - startTime;
logger.performance('Schema introspection', duration, {
types,
queries,
mutations,
subscriptions,
format: params.format,
});
return {
schema: schemaOutput,
format: params.format,
types,
queries,
mutations,
subscriptions,
version: schemaInfo.version,
lastUpdated: schemaInfo.lastUpdated.toISOString(),
};
}
catch (error) {
const duration = Date.now() - startTime;
logger.error('Schema introspection failed', {
error: error instanceof Error ? error.message : 'Unknown error',
duration,
});
throw error;
}
};
exports.handleIntrospectSchema = handleIntrospectSchema;
//# sourceMappingURL=introspect-schema.js.map