The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.
TypeScript agentai-chatai-studioall-in-onecoding-agentdesktop-appfluximage-generationlinuxllmlocal-ailocal-llmopen-sourceprivacyself-hostedstable-diffusionuncensoreduncensored-aivideo-generationwindows
Phase 8: MCP wiring — external tools dispatch correctly (v2.4.0)
PurpleDoubleDf0022fab12f4
diff --git a/src/api/mcp/__tests__/tool-registry-external.test.ts b/src/api/mcp/__tests__/tool-registry-external.test.tsnew file mode 100644index 00000000..960d5f3e--- /dev/null+++ b/src/api/mcp/__tests__/tool-registry-external.test.ts@@ -0,0 +1,124 @@+import { describe, it, expect, vi } from 'vitest'+import { ToolRegistry } from '../tool-registry'+import { resolveCommandForPlatform } from '../external-client'+import type { MCPToolDefinition, PermissionMap } from '../types'++const fullPerms: PermissionMap = {+ filesystem: 'auto',+ terminal: 'auto',+ desktop: 'auto',+ web: 'auto',+ system: 'auto',+ image: 'auto',+ video: 'auto',+ workflow: 'auto',+}++const mkTool = (name: string, extra: Partial<MCPToolDefinition> = {}): MCPToolDefinition => ({+ name,+ description: `description of ${name}`,+ inputSchema: { type: 'object', properties: {}, required: [] },+ category: 'workflow',+ source: 'external',+ ...extra,+})++describe('ToolRegistry — registerExternal name binding', () => {+ it('routes each tool call to the right tool name via the two-arg executor', async () => {+ const registry = new ToolRegistry()+ const calls: { name: string; args: any }[] = []+ registry.registerExternal(+ 'srv1',+ [mkTool('search'), mkTool('read'), mkTool('write')],+ async (name, args) => {+ calls.push({ name, args })+ return `invoked:${name}`+ }+ )+ expect(await registry.execute('search', { q: 'x' })).toBe('invoked:search')+ expect(await registry.execute('read', { path: 'a' })).toBe('invoked:read')+ expect(await registry.execute('write', { path: 'b' })).toBe('invoked:write')+ expect(calls.map((c) => c.name)).toEqual(['search', 'read', 'write'])+ })++ it('supports legacy single-arg executor (backcompat)', async () => {+ const registry = new ToolRegistry()+ const legacy = vi.fn(async (_args: Record<string, any>) => 'legacy-ok')+ registry.registerExternal('srv2', [mkTool('only')], legacy)+ expect(await registry.execute('only', {})).toBe('legacy-ok')+ expect(legacy).toHaveBeenCalledOnce()+ })++ it('tags external tools with serverId + source', () => {+ const registry = new ToolRegistry()+ registry.registerExternal('srv3', [mkTool('t')], async () => '')+ const t = registry.getToolByName('t')!+ expect(t.serverId).toBe('srv3')+ expect(t.source).toBe('external')+ })++ it('unregisterServer drops only that server\'s tools', () => {+ const registry = new ToolRegistry()+ registry.registerExternal('s1', [mkTool('a'), mkTool('b')], async () => '')+ registry.registerExternal('s2', [mkTool('c')], async () => '')+ registry.unregisterServer('s1')+ expect(registry.getToolByName('a')).toBeUndefined()+ expect(registry.getToolByName('b')).toBeUndefined()+ expect(registry.getToolByName('c')).toBeDefined()+ })++ it('external tools appear in toOpenAITools / toOllamaTools alongside builtins', () => {+ const registry = new ToolRegistry()+ registry.registerBuiltin(+ { ...mkTool('builtin_a'), source: 'builtin' },+ async () => 'ok'+ )+ registry.registerExternal('srv', [mkTool('external_a')], async () => 'ok')+ const openai = registry.toOpenAITools(fullPerms).map((t) => t.function.name).sort()+ const ollama = registry.toOllamaTools(fullPerms).map((t) => t.function.name).sort()+ expect(openai).toEqual(['builtin_a', 'external_a'])+ expect(ollama).toEqual(['builtin_a', 'external_a'])+ })++ it('external tool failure propagates as Error: string (retry-friendly)', async () => {+ const registry = new ToolRegistry()+ registry.registerExternal('srv', [mkTool('broken')], async () => {+ throw new Error('connection lost')+ })+ const out = await registry.execute('broken', {})+ expect(out).toMatch(/Error: connection lost/)+ })+})++describe('external-client — resolveCommandForPlatform', () => {+ it('appends .cmd for known Node-based commands on Windows', () => {+ expect(resolveCommandForPlatform('npx', 'Win32')).toBe('npx.cmd')+ expect(resolveCommandForPlatform('npm', 'Win32')).toBe('npm.cmd')+ expect(resolveCommandForPlatform('pnpm', 'Win32')).toBe('pnpm.cmd')+ expect(resolveCommandForPlatform('yarn', 'Win32')).toBe('yarn.cmd')+ expect(resolveCommandForPlatform('bun', 'Win32')).toBe('bun.cmd')+ expect(resolveCommandForPlatform('node', 'Win32')).toBe('node.cmd')+ expect(resolveCommandForPlatform('deno', 'Win32')).toBe('deno.cmd')+ })++ it('leaves commands with extensions unchanged on Windows', () => {+ expect(resolveCommandForPlatform('npx.cmd', 'Win32')).toBe('npx.cmd')+ expect(resolveCommandForPlatform('mcp-server.exe', 'Win32')).toBe('mcp-server.exe')+ })++ it('leaves absolute / path-like commands unchanged on Windows', () => {+ expect(resolveCommandForPlatform('C:\\Program Files\\node\\node.exe', 'Win32')).toBe(+ 'C:\\Program Files\\node\\node.exe'+ )+ expect(resolveCommandForPlatform('./bin/server', 'Win32')).toBe('./bin/server')+ })++ it('leaves unknown commands unchanged on Windows', () => {+ expect(resolveCommandForPlatform('some-custom-mcp', 'Win32')).toBe('some-custom-mcp')+ })++ it('returns command unchanged on non-Windows platforms', () => {+ expect(resolveCommandForPlatform('npx', 'MacIntel')).toBe('npx')+ expect(resolveCommandForPlatform('npx', 'Linux x86_64')).toBe('npx')+ })+})diff --git a/src/api/mcp/external-client.ts b/src/api/mcp/external-client.tsindex 2bfd90ca..60d806a1 100644--- a/src/api/mcp/external-client.ts+++ b/src/api/mcp/external-client.ts@@ -41,7 +41,14 @@ export class MCPExternalClient {const shellModule = await import(/* @vite-ignore */ '@tauri-apps/plugin-shell')const { Command } = shellModule- this.process = Command.create(this.config.command, this.config.args, {+ // Windows gotcha: Node-based MCP servers are invoked as `npx`, `npm`,+ // `node` — but on Windows the resolvable PATH entries for those are+ // `npx.cmd`, `npm.cmd`, etc. Command.create tries to spawn the bare+ // name which fails with "program not found". Map known command names+ // to their .cmd shim here.+ const resolvedCommand = resolveCommandForPlatform(this.config.command)++ this.process = Command.create(resolvedCommand, this.config.args, {env: this.config.env,})@@ -181,3 +188,20 @@ export class MCPExternalClient {}}}++/**+ * Map a bare command name to the Windows `.cmd` shim when running on+ * Windows. Commands with an extension or an absolute path are returned+ * unchanged.+ */+export function resolveCommandForPlatform(+ command: string,+ platform: string = typeof navigator !== 'undefined' ? navigator.platform : ''+): string {+ const isWindows = /Win/i.test(platform)+ if (!isWindows) return command+ if (/[\\/]|\.(cmd|bat|exe)$/i.test(command)) return command+ const NEEDS_CMD = new Set(['npx', 'npm', 'pnpm', 'yarn', 'bun', 'node', 'deno'])+ if (NEEDS_CMD.has(command)) return `${command}.cmd`+ return command+}diff --git a/src/api/mcp/tool-registry.ts b/src/api/mcp/tool-registry.tsindex db20db5a..b777804c 100644--- a/src/api/mcp/tool-registry.ts+++ b/src/api/mcp/tool-registry.ts@@ -5,6 +5,12 @@ import type { OllamaTool } from '../../types/agent-mode'import type { ToolDefinition } from '../providers/types'type ToolExecutor = (args: Record<string, any>) => Promise<string>+/**+ * External-tool executor gets the tool name too, because one MCP server+ * owns many tools and routes by name. The registry wraps it into a+ * per-tool ToolExecutor closure so the Map lookup stays {name → executor}.+ */+type ExternalToolExecutor = (toolName: string, args: Record<string, any>) => Promise<string>interface RegisteredTool {definition: MCPToolDefinition@@ -20,11 +26,31 @@ export class ToolRegistry {this.tools.set(tool.name, { definition: tool, executor })}- registerExternal(serverId: string, tools: MCPToolDefinition[], executor: ToolExecutor) {+ /**+ * Register all tools from an external MCP server. The executor receives+ * both the tool name and args, letting one MCP client back many tools+ * (which is how MCP servers actually work — one `tools/call` endpoint,+ * dispatched by name).+ *+ * Also accepts the legacy single-arg executor shape for backward compat+ * — tests and older callers can still pass `(args) => ...` and the+ * registry will call it unchanged. Prefer the two-arg shape in new code.+ */+ registerExternal(+ serverId: string,+ tools: MCPToolDefinition[],+ executor: ExternalToolExecutor | ToolExecutor+ ) {+ const isTwoArg = executor.length >= 2for (const tool of tools) {- this.tools.set(tool.name, {+ const name = tool.name+ const bound: ToolExecutor = isTwoArg+ ? (args: Record<string, any>) =>+ (executor as ExternalToolExecutor)(name, args)+ : (executor as ToolExecutor)+ this.tools.set(name, {definition: { ...tool, source: 'external', serverId },- executor,+ executor: bound,})}}diff --git a/src/components/settings/MCPServerSettings.tsx b/src/components/settings/MCPServerSettings.tsxindex c4a603b2..0a9eb403 100644--- a/src/components/settings/MCPServerSettings.tsx+++ b/src/components/settings/MCPServerSettings.tsx@@ -45,15 +45,15 @@ export function MCPServerSettings() {clients.set(server.id, client)setConnected(server.id, true)setServerTools(server.id, tools)- // Register tools with the global registry+ // Register tools with the global registry. The two-arg executor+ // contract lets the registry bind each tool's name into its own+ // closure — previously a single-arg hack tried to smuggle the name+ // via `args.__toolName`, which was never populated, so MCP calls+ // silently dispatched with an empty tool name and failed.toolRegistry.registerExternal(server.id,tools,- async (args) => {- const toolName = args.__toolName || ''- delete args.__toolName- return client.callTool(toolName, args)- }+ async (toolName, args) => client.callTool(toolName, args))} catch (err) {setError(err instanceof Error ? err.message : String(err))