@gebrai/gebrai
Version:
Model Context Protocol server for GeoGebra mathematical visualization
168 lines • 5.15 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.toolRegistry = exports.ToolRegistry = void 0;
const errors_1 = require("../utils/errors");
const logger_1 = __importDefault(require("../utils/logger"));
const geogebra_tools_1 = require("./geogebra-tools");
const educational_templates_1 = require("./educational-templates");
const performance_tools_1 = require("./performance-tools");
// Check if we're in MCP mode (stdio communication)
// When piping input, process.stdin.isTTY is undefined, not false
const isMcpMode = !process.stdin.isTTY;
/**
* Tool Registry for managing MCP tools
*/
class ToolRegistry {
tools = new Map();
/**
* Register a new tool
*/
register(definition) {
this.tools.set(definition.tool.name, definition);
if (!isMcpMode) {
logger_1.default.info(`Registered tool: ${definition.tool.name}`);
}
}
/**
* Get all registered tools
*/
getTools() {
return Array.from(this.tools.values()).map(def => def.tool);
}
/**
* Get a specific tool by name
*/
getTool(name) {
return this.tools.get(name);
}
/**
* Execute a tool by name with given parameters
*/
async executeTool(name, params = {}) {
const toolDef = this.tools.get(name);
if (!toolDef) {
throw errors_1.errors.toolNotFound(name);
}
try {
if (!isMcpMode) {
logger_1.default.info(`Executing tool: ${name}`, { params });
}
const result = await toolDef.handler(params);
if (!isMcpMode) {
logger_1.default.info(`Tool execution completed: ${name}`);
}
return result;
}
catch (error) {
logger_1.default.error(`Tool execution failed: ${name}`, error);
throw errors_1.errors.toolExecutionError(name, error instanceof Error ? error.message : String(error));
}
}
/**
* Check if a tool exists
*/
hasTool(name) {
return this.tools.has(name);
}
/**
* Get the number of registered tools
*/
getToolCount() {
return this.tools.size;
}
}
exports.ToolRegistry = ToolRegistry;
// Create global tool registry instance
exports.toolRegistry = new ToolRegistry();
// Example tools for testing and demonstration
const exampleTools = [
{
tool: {
name: 'echo',
description: 'Echo back the provided message',
inputSchema: {
type: 'object',
properties: {
message: {
type: 'string',
description: 'The message to echo back'
}
},
required: ['message']
}
},
handler: async (params) => {
const message = params['message'];
return {
content: [{
type: 'text',
text: `Echo: ${message}`
}]
};
}
},
{
tool: {
name: 'ping',
description: 'Simple ping tool that returns pong',
inputSchema: {
type: 'object',
properties: {},
required: []
}
},
handler: async () => {
return {
content: [{
type: 'text',
text: 'pong'
}]
};
}
},
{
tool: {
name: 'server_info',
description: 'Get information about the MCP server',
inputSchema: {
type: 'object',
properties: {},
required: []
}
},
handler: async () => {
return {
content: [{
type: 'text',
text: JSON.stringify({
name: 'GeoGebra MCP Tool',
version: '1.0.0',
description: 'Model Context Protocol server for GeoGebra mathematical visualization',
toolCount: exports.toolRegistry.getToolCount(),
timestamp: new Date().toISOString()
}, null, 2)
}]
};
}
}
];
// Register example tools
exampleTools.forEach(tool => {
exports.toolRegistry.register(tool);
});
// Register GeoGebra tools
geogebra_tools_1.geogebraTools.forEach(tool => {
exports.toolRegistry.register(tool);
});
// Register Educational Template tools
educational_templates_1.educationalTemplateTools.forEach(tool => {
exports.toolRegistry.register(tool);
});
// Register Performance tools
performance_tools_1.performanceTools.forEach(tool => {
exports.toolRegistry.register(tool);
});
//# sourceMappingURL=index.js.map