Skip to main content
Glama
ToolsController.ts3.83 kB
import { Request, Response } from 'express'; import { ToolRegistry } from '../tools/ToolRegistry'; import { policyEngine } from '../index'; import { PolicyEvaluationContext } from '../models/Policy'; export class ToolsController { private toolRegistry: ToolRegistry; constructor(toolRegistry: ToolRegistry) { this.toolRegistry = toolRegistry; } /** * List all available tools * @param req Request * @param res Response */ public listTools(req: Request, res: Response): void { try { // Create policy evaluation context const context: PolicyEvaluationContext = { sessionId: req.headers['x-session-id'] as string || 'unknown', ipAddress: req.ip || 'unknown', resource: 'tools', action: 'fileAccess', timestamp: new Date() }; // Check policy if (!policyEngine.isActionAllowed(context)) { res.status(403).json({ error: 'Access denied by policy' }); return; } const tools = this.toolRegistry.getAllTools().map(tool => ({ name: tool.name, description: tool.description, parameters: tool.parameters })); res.status(200).json({ tools }); } catch (error) { res.status(500).json({ error: 'Failed to list tools' }); } } /** * Get tool information * @param req Request with tool name * @param res Response with tool information */ public getTool(req: Request, res: Response): void { try { const toolName = req.params.toolName; // Create policy evaluation context const context: PolicyEvaluationContext = { sessionId: req.headers['x-session-id'] as string || 'unknown', ipAddress: req.ip || 'unknown', resource: `tools/${toolName}`, action: 'fileAccess', timestamp: new Date() }; // Check policy if (!policyEngine.isActionAllowed(context)) { res.status(403).json({ error: 'Access denied by policy' }); return; } const tool = this.toolRegistry.getTool(toolName); if (!tool) { res.status(404).json({ error: 'Tool not found' }); return; } res.status(200).json({ name: tool.name, description: tool.description, parameters: tool.parameters }); } catch (error) { res.status(500).json({ error: 'Failed to get tool' }); } } /** * Execute a tool * @param req Request with tool name and arguments * @param res Response with tool result */ public executeTool(req: Request, res: Response): void { try { const toolName = req.params.toolName; const args = req.body; // Create policy evaluation context const context: PolicyEvaluationContext = { sessionId: req.headers['x-session-id'] as string || 'unknown', ipAddress: req.ip || 'unknown', resource: `tools/${toolName}`, action: 'commandExecution', timestamp: new Date() }; // Check policy if (!policyEngine.isActionAllowed(context)) { res.status(403).json({ error: 'Access denied by policy' }); return; } // Check if tool exists const tool = this.toolRegistry.getTool(toolName); if (!tool) { res.status(404).json({ error: 'Tool not found' }); return; } // Execute the tool this.toolRegistry.executeTool(toolName, args) .then(result => { res.status(200).json(result); }) .catch(error => { res.status(500).json({ error: 'Failed to execute tool', message: error.message }); }); } catch (error) { res.status(500).json({ error: 'Failed to execute tool' }); } } }

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/Nom-nom-hub/fullstack-mcp'

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