()
| 44 | }; |
| 45 | |
| 46 | export const createMcpServer = (): McpServer => { |
| 47 | |
| 48 | const server = new McpServer({ |
| 49 | name: "mobile-mcp", |
| 50 | version: getAgentVersion(), |
| 51 | }); |
| 52 | |
| 53 | |
| 54 | const getClientName = (): string => { |
| 55 | try { |
| 56 | const clientInfo = server.server.getClientVersion(); |
| 57 | const clientName = clientInfo?.name || "unknown"; |
| 58 | return clientName; |
| 59 | } catch (error: any) { |
| 60 | return "unknown"; |
| 61 | } |
| 62 | }; |
| 63 | |
| 64 | type ZodSchemaShape = Record<string, z.ZodType>; |
| 65 | |
| 66 | interface ToolAnnotations { |
| 67 | readOnlyHint?: boolean; |
| 68 | destructiveHint?: boolean; |
| 69 | } |
| 70 | |
| 71 | const tool = (name: string, title: string, description: string, paramsSchema: ZodSchemaShape, annotations: ToolAnnotations, cb: (args: any) => Promise<string>) => { |
| 72 | server.registerTool(name, { |
| 73 | title, |
| 74 | description, |
| 75 | inputSchema: paramsSchema, |
| 76 | annotations, |
| 77 | }, (async (args: any, _extra: any) => { |
| 78 | try { |
| 79 | trace(`Invoking ${name} with args: ${JSON.stringify(args)}`); |
| 80 | const start = +new Date(); |
| 81 | const response = await cb(args); |
| 82 | const duration = +new Date() - start; |
| 83 | trace(`=> ${response}`); |
| 84 | posthog("tool_invoked", { "ToolName": name, "Duration": duration }).then(); |
| 85 | return { |
| 86 | content: [{ type: "text", text: response }], |
| 87 | }; |
| 88 | } catch (error: any) { |
| 89 | posthog("tool_failed", { "ToolName": name }).then(); |
| 90 | if (error instanceof ActionableError) { |
| 91 | return { |
| 92 | content: [{ type: "text", text: `${error.message}. Please fix the issue and try again.` }], |
| 93 | }; |
| 94 | } else { |
| 95 | // a real exception |
| 96 | trace(`Tool '${description}' failed: ${error.message} stack: ${error.stack}`); |
| 97 | return { |
| 98 | content: [{ type: "text", text: `Error: ${error.message}` }], |
| 99 | isError: true, |
| 100 | }; |
| 101 | } |
| 102 | } |
| 103 | }) as any); |
no test coverage detected