Skip to main content
Glama
by fmangot
handlers.ts3.66 kB
/** * MCP Tool Handlers * Shared handler logic for all server implementations */ import { SequentialThinkingManager } from '../lib.js'; import { ThoughtInput } from '../types.js'; import { ValidationError } from '../utils/validators.js'; /** * Handle tool calls with a thinking manager */ export async function handleToolCall( params: { name: string; arguments?: unknown }, thinkingManager: SequentialThinkingManager ) { const { name, arguments: args = {} } = params; try { switch (name) { case 'sequential_thinking': { const input = args as ThoughtInput; const result = thinkingManager.addThought(input); return { content: [ { type: 'text', text: JSON.stringify(result, null, 2), }, ], }; } case 'get_thought_sequence': { const { sessionId } = args as { sessionId?: string }; const sequence = thinkingManager.getSequence(sessionId); return { content: [ { type: 'text', text: JSON.stringify( { sessionId: sessionId || thinkingManager.getCurrentSessionId(), thoughtCount: sequence.length, thoughts: sequence, }, null, 2 ), }, ], }; } case 'get_thought_branch': { const { branchId, sessionId } = args as { branchId: string; sessionId?: string }; if (!branchId) { throw new ValidationError('branchId is required'); } const branch = thinkingManager.getBranch(branchId, sessionId); return { content: [ { type: 'text', text: JSON.stringify( { branchId, thoughtCount: branch.length, thoughts: branch, }, null, 2 ), }, ], }; } case 'reset_thinking_session': { const newSessionId = thinkingManager.resetSession(); return { content: [ { type: 'text', text: JSON.stringify( { message: 'New thinking session started', sessionId: newSessionId, }, null, 2 ), }, ], }; } case 'get_session_summary': { const { sessionId } = args as { sessionId?: string }; const summary = thinkingManager.getSessionSummary(sessionId); if (!summary) { throw new Error('Session not found'); } return { content: [ { type: 'text', text: JSON.stringify(summary, null, 2), }, ], }; } default: throw new Error(`Unknown tool: ${name}`); } } catch (error) { console.error(`Error in tool ${name}:`, error); const errorMessage = error instanceof Error ? error.message : 'Unknown error'; const isValidationError = error instanceof ValidationError; return { content: [ { type: 'text', text: JSON.stringify( { error: isValidationError ? errorMessage : 'Internal server error', code: isValidationError ? 'VALIDATION_ERROR' : 'INTERNAL_ERROR', }, null, 2 ), }, ], isError: true, }; } }

Implementation Reference

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/fmangot/Mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server