File size: 2,890 Bytes
d07c56c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import { CallToolRequestSchema, ListToolsRequestSchema } from '@modelcontextprotocol/sdk/types.js';
import { z } from 'zod';
import { executeTemplateAgentAction } from './runtime.js';

const onboardingSaveSchema = z.object({
  ownerEmail: z.string().optional(),
  notes: z.string().optional(),
  confirm: z.boolean().optional(),
});

const TOOLS = [
  {
    name: 'sin-code-datascience_help',
    description: 'Describe available agent actions.',
    inputSchema: { type: 'object', properties: {} },
    parse: () => ({}),
    handler: async () => executeTemplateAgentAction({ action: 'agent.help' }),
  },
  {
    name: 'sin-code-datascience_health',
    description: 'Check base agent readiness.',
    inputSchema: { type: 'object', properties: {} },
    parse: () => ({}),
    handler: async () => executeTemplateAgentAction({ action: 'sin.code.datascience.health' }),
  },
  {
    name: 'sin-code-datascience_onboarding_status',
    description: 'Read onboarding state.',
    inputSchema: { type: 'object', properties: {} },
    parse: () => ({}),
    handler: async () => executeTemplateAgentAction({ action: 'sin.code.datascience.onboarding.status' }),
  },
  {
    name: 'sin-code-datascience_onboarding_save',
    description: 'Persist onboarding state. Requires confirm=true.',
    inputSchema: {
      type: 'object',
      properties: {
        ownerEmail: { type: 'string' },
        notes: { type: 'string' },
        confirm: { type: 'boolean' },
      },
    },
    parse: (input: unknown) => onboardingSaveSchema.parse(input ?? {}),
    handler: async (input: z.infer<typeof onboardingSaveSchema>) =>
      executeTemplateAgentAction({ action: 'sin.code.datascience.onboarding.save', ...input }),
  },
] as const;

export async function startMcpServer(): Promise<void> {
  const server = new Server({ name: 'sin-code-datascience', version: '0.1.0' }, { capabilities: { tools: {} } });

  server.setRequestHandler(ListToolsRequestSchema, async () => {
    return {
      tools: TOOLS.map((tool) => ({
        name: tool.name,
        description: tool.description,
        inputSchema: tool.inputSchema,
      })),
    };
  });

  server.setRequestHandler(CallToolRequestSchema, async (request) => {
    const tool = TOOLS.find((entry) => entry.name === request.params.name);
    if (!tool) {
      return {
        content: [{ type: 'text', text: `Unknown tool: ${request.params.name}` }],
        isError: true,
      };
    }
    const parsed = tool.parse(request.params.arguments ?? {});
    const result = await tool.handler(parsed);
    return {
      content: [{ type: 'text', text: JSON.stringify(result, null, 2) }],
    };
  });

  const transport = new StdioServerTransport();
  await server.connect(transport);
}