purchase-mcp-server
Version:
Purchase and budget management server handling requisitions, purchase orders, expenses, budgets, and vendor management with ERP access for data extraction
130 lines (126 loc) • 4.42 kB
JavaScript
/**
* Schema-dependent TypeScript interface generator
* Automatically generates TypeScript interfaces from tool schemas
*/
import { toolDefinitions } from "../tools/schema.js";
/**
* Convert JSON Schema property to TypeScript type string
*/
function convertPropertyToTypeScript(property) {
let typeString = "";
switch (property.type) {
case "string":
if (property.enum) {
typeString = property.enum.map(value => `"${value}"`).join(" | ");
}
else if (property.format === "date") {
typeString = "string"; // ISO date string
}
else {
typeString = "string";
}
break;
case "number":
typeString = "number";
break;
case "integer":
typeString = "number";
break;
case "boolean":
typeString = "boolean";
break;
case "array":
if (property.items) {
const itemType = convertPropertyToTypeScript(property.items);
typeString = `${itemType}[]`;
}
else {
typeString = "any[]";
}
break;
case "object":
if (property.properties) {
const objectProperties = [];
for (const [propName, propDef] of Object.entries(property.properties)) {
const propTypeString = convertPropertyToTypeScript(propDef);
objectProperties.push(` ${propName}: ${propTypeString};`);
}
typeString = `{\n${objectProperties.join('\n')}\n }`;
}
else {
typeString = "Record<string, any>";
}
break;
default:
typeString = "any";
}
return typeString;
}
/**
* Format property definition for interface
*/
function formatPropertyForInterface(propertyName, property, isRequired = false) {
const optional = isRequired ? "" : "?";
const typeString = convertPropertyToTypeScript(property);
return ` ${propertyName}${optional}: ${typeString};`;
}
/**
* Generate interface name from tool name
*/
function generateInterfaceName(toolName) {
return toolName
.split('_')
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
.join('') + 'Arguments';
}
/**
* Generate TypeScript interface from tool schema
*/
function generateInterface(tool) {
const interfaceName = generateInterfaceName(tool.name);
const schema = tool.inputSchema;
const required = schema.required || [];
const properties = [];
for (const [propertyName, property] of Object.entries(schema.properties)) {
// Skip session_id as it's already in BaseToolArguments
if (propertyName === 'session_id')
continue;
const isRequired = required.includes(propertyName);
const propertyString = formatPropertyForInterface(propertyName, property, isRequired);
properties.push(propertyString);
}
return `
export interface ${interfaceName} extends BaseToolArguments {
${properties.join('\n')}
}`;
}
/**
* Generate complete TypeScript definitions file
*/
export function generateTypeScriptDefinitions() {
const header = `/**
* Auto-generated TypeScript interfaces from tool schemas
* DO NOT EDIT MANUALLY - This file is generated from src/tools/schema.ts
*/
import { TextContent, ImageContent, EmbeddedResource } from "@modelcontextprotocol/sdk/types.js";
export type ToolResponse = Array<TextContent | ImageContent | EmbeddedResource>;
// Base interface for common properties
interface BaseToolArguments {
session_id?: string;
}`;
const interfaces = toolDefinitions.map(tool => generateInterface(tool)).join('\n');
// Generate the tool name to interface mapping
const mappings = toolDefinitions.map(tool => {
const interfaceName = generateInterfaceName(tool.name);
return ` "${tool.name}": ${interfaceName};`;
}).join('\n');
const footer = `
// Tool name to interface mapping
export type ToolArgumentsMap = {
${mappings}
};
// Helper type to get arguments for a specific tool
export type GetToolArguments<T extends keyof ToolArgumentsMap> = ToolArgumentsMap[T];`;
return `${header}\n${interfaces}\n${footer}`;
}
//# sourceMappingURL=generator.js.map