|
| 1 | +// SPDX-License-Identifier: PMPL-1.0-or-later |
| 2 | +// Fireflag Cartridge — Extension-to-MCP mapping MCP server |
| 3 | + |
| 4 | +import { Server } from "https://esm.sh/@modelcontextprotocol/sdk/server/index.js"; |
| 5 | +import { |
| 6 | + CallToolRequestSchema, |
| 7 | + ListToolsRequestSchema, |
| 8 | + Tool, |
| 9 | +} from "https://esm.sh/@modelcontextprotocol/sdk/types.js"; |
| 10 | + |
| 11 | +// MCP tool definitions for extension mapping |
| 12 | +const TOOLS: Tool[] = [ |
| 13 | + { |
| 14 | + name: "map_extension", |
| 15 | + description: "Map an extension directory to available MCP tools", |
| 16 | + inputSchema: { |
| 17 | + type: "object" as const, |
| 18 | + properties: { |
| 19 | + extension_path: { |
| 20 | + type: "string", |
| 21 | + description: "Path to extension directory", |
| 22 | + }, |
| 23 | + }, |
| 24 | + required: ["extension_path"], |
| 25 | + }, |
| 26 | + }, |
| 27 | + { |
| 28 | + name: "list_mapped_extensions", |
| 29 | + description: "List all mapped extensions with their MCP capabilities", |
| 30 | + inputSchema: { |
| 31 | + type: "object" as const, |
| 32 | + properties: {}, |
| 33 | + }, |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "get_extension_tools", |
| 37 | + description: "Get available MCP tools for a specific extension", |
| 38 | + inputSchema: { |
| 39 | + type: "object" as const, |
| 40 | + properties: { |
| 41 | + extension_id: { |
| 42 | + type: "string", |
| 43 | + description: "Extension identifier", |
| 44 | + }, |
| 45 | + }, |
| 46 | + required: ["extension_id"], |
| 47 | + }, |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "validate_extension", |
| 51 | + description: "Validate extension configuration and structure", |
| 52 | + inputSchema: { |
| 53 | + type: "object" as const, |
| 54 | + properties: { |
| 55 | + extension_path: { |
| 56 | + type: "string", |
| 57 | + description: "Path to extension to validate", |
| 58 | + }, |
| 59 | + }, |
| 60 | + required: ["extension_path"], |
| 61 | + }, |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "discover_extensions", |
| 65 | + description: "Discover extensions in a directory and map their capabilities", |
| 66 | + inputSchema: { |
| 67 | + type: "object" as const, |
| 68 | + properties: { |
| 69 | + directory: { |
| 70 | + type: "string", |
| 71 | + description: "Directory to search for extensions", |
| 72 | + }, |
| 73 | + }, |
| 74 | + required: ["directory"], |
| 75 | + }, |
| 76 | + }, |
| 77 | +]; |
| 78 | + |
| 79 | +// Tool handlers |
| 80 | +async function handleMapExtension( |
| 81 | + args: Record<string, unknown> |
| 82 | +): Promise<string> { |
| 83 | + const extensionPath = String(args.extension_path); |
| 84 | + return JSON.stringify({ |
| 85 | + extension_path: extensionPath, |
| 86 | + is_mapped: false, |
| 87 | + mapping_status: "not_found", |
| 88 | + metadata: null, |
| 89 | + }); |
| 90 | +} |
| 91 | + |
| 92 | +async function handleListMappedExtensions( |
| 93 | + _args: Record<string, unknown> |
| 94 | +): Promise<string> { |
| 95 | + return JSON.stringify({ |
| 96 | + extensions: [], |
| 97 | + count: 0, |
| 98 | + }); |
| 99 | +} |
| 100 | + |
| 101 | +async function handleGetExtensionTools( |
| 102 | + args: Record<string, unknown> |
| 103 | +): Promise<string> { |
| 104 | + const extensionId = String(args.extension_id); |
| 105 | + return JSON.stringify({ |
| 106 | + extension_id: extensionId, |
| 107 | + tools: [], |
| 108 | + count: 0, |
| 109 | + }); |
| 110 | +} |
| 111 | + |
| 112 | +async function handleValidateExtension( |
| 113 | + args: Record<string, unknown> |
| 114 | +): Promise<string> { |
| 115 | + const extensionPath = String(args.extension_path); |
| 116 | + return JSON.stringify({ |
| 117 | + extension_path: extensionPath, |
| 118 | + is_valid: true, |
| 119 | + errors: [], |
| 120 | + warnings: [], |
| 121 | + }); |
| 122 | +} |
| 123 | + |
| 124 | +async function handleDiscoverExtensions( |
| 125 | + args: Record<string, unknown> |
| 126 | +): Promise<string> { |
| 127 | + const directory = String(args.directory); |
| 128 | + return JSON.stringify({ |
| 129 | + directory, |
| 130 | + extensions: [], |
| 131 | + count: 0, |
| 132 | + }); |
| 133 | +} |
| 134 | + |
| 135 | +// Initialize MCP server |
| 136 | +const server = new Server({ |
| 137 | + name: "fireflag-mcp", |
| 138 | + version: "1.0.0", |
| 139 | +}); |
| 140 | + |
| 141 | +// Register tool handlers |
| 142 | +server.setRequestHandler(ListToolsRequestSchema, async () => { |
| 143 | + return { tools: TOOLS }; |
| 144 | +}); |
| 145 | + |
| 146 | +server.setRequestHandler(CallToolRequestSchema, async (request) => { |
| 147 | + const { name, arguments: args } = request; |
| 148 | + |
| 149 | + let result: string; |
| 150 | + if (name === "map_extension") { |
| 151 | + result = await handleMapExtension(args as Record<string, unknown>); |
| 152 | + } else if (name === "list_mapped_extensions") { |
| 153 | + result = await handleListMappedExtensions(args as Record<string, unknown>); |
| 154 | + } else if (name === "get_extension_tools") { |
| 155 | + result = await handleGetExtensionTools(args as Record<string, unknown>); |
| 156 | + } else if (name === "validate_extension") { |
| 157 | + result = await handleValidateExtension(args as Record<string, unknown>); |
| 158 | + } else if (name === "discover_extensions") { |
| 159 | + result = await handleDiscoverExtensions(args as Record<string, unknown>); |
| 160 | + } else { |
| 161 | + return { |
| 162 | + content: [ |
| 163 | + { |
| 164 | + type: "text" as const, |
| 165 | + text: `Unknown tool: ${name}`, |
| 166 | + }, |
| 167 | + ], |
| 168 | + isError: true, |
| 169 | + }; |
| 170 | + } |
| 171 | + |
| 172 | + return { |
| 173 | + content: [ |
| 174 | + { |
| 175 | + type: "text" as const, |
| 176 | + text: result, |
| 177 | + }, |
| 178 | + ], |
| 179 | + }; |
| 180 | +}); |
| 181 | + |
| 182 | +// Start server on loopback |
| 183 | +const port = 5177; |
| 184 | +await server.connect(new WebSocket(`ws://127.0.0.1:${port}`)); |
| 185 | +console.log("Fireflag MCP server running on ws://127.0.0.1:5177"); |
0 commit comments