|
1 | 1 | import type { Config } from '@doc-agent/core'; |
2 | 2 | import { extractDocument } from '@doc-agent/extract'; |
3 | | -import { Server } from '@modelcontextprotocol/sdk/server'; |
4 | | -import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'; |
5 | | -import { |
6 | | - type CallToolRequest, |
7 | | - CallToolRequestSchema, |
8 | | - ListToolsRequestSchema, |
9 | | -} from '@modelcontextprotocol/sdk/types.js'; |
| 3 | +import { z } from 'zod'; |
| 4 | +import { McpServer, StdioServerTransport } from './sdk'; |
10 | 5 |
|
11 | | -const server = new Server( |
12 | | - { |
13 | | - name: 'doc-agent', |
14 | | - version: '0.1.0', |
15 | | - }, |
16 | | - { |
17 | | - capabilities: { |
18 | | - tools: {}, |
19 | | - }, |
20 | | - } |
21 | | -); |
22 | | - |
23 | | -// List available tools |
24 | | -server.setRequestHandler(ListToolsRequestSchema, async () => { |
25 | | - return { |
26 | | - tools: [ |
27 | | - { |
28 | | - name: 'extract_document', |
29 | | - description: 'Extract structured data from invoice, receipt, or bank statement', |
30 | | - inputSchema: { |
31 | | - type: 'object', |
32 | | - properties: { |
33 | | - filepath: { |
34 | | - type: 'string', |
35 | | - description: 'Path to the document file', |
36 | | - }, |
37 | | - provider: { |
38 | | - type: 'string', |
39 | | - enum: ['gemini', 'openai', 'ollama'], |
40 | | - description: 'AI provider to use', |
41 | | - default: 'gemini', |
42 | | - }, |
43 | | - }, |
44 | | - required: ['filepath'], |
45 | | - }, |
46 | | - }, |
47 | | - { |
48 | | - name: 'search_documents', |
49 | | - description: 'Search indexed documents using natural language', |
50 | | - inputSchema: { |
51 | | - type: 'object', |
52 | | - properties: { |
53 | | - query: { |
54 | | - type: 'string', |
55 | | - description: 'Search query in natural language', |
56 | | - }, |
57 | | - limit: { |
58 | | - type: 'number', |
59 | | - description: 'Maximum number of results', |
60 | | - default: 10, |
61 | | - }, |
62 | | - }, |
63 | | - required: ['query'], |
64 | | - }, |
65 | | - }, |
66 | | - ], |
67 | | - }; |
| 6 | +const server = new McpServer({ |
| 7 | + name: 'doc-agent', |
| 8 | + version: '0.1.0', |
68 | 9 | }); |
69 | 10 |
|
70 | | -// Handle tool calls |
71 | | -server.setRequestHandler(CallToolRequestSchema, async (request: CallToolRequest) => { |
72 | | - if (request.params.name === 'extract_document') { |
73 | | - const { filepath, provider = 'gemini' } = request.params.arguments as { |
74 | | - filepath: string; |
75 | | - provider?: string; |
76 | | - }; |
77 | | - |
| 11 | +// Register extract_document tool |
| 12 | +server.registerTool( |
| 13 | + 'extract_document', |
| 14 | + { |
| 15 | + description: 'Extract structured data from invoice, receipt, or bank statement', |
| 16 | + inputSchema: { |
| 17 | + filepath: z.string().describe('Path to the document file'), |
| 18 | + provider: z |
| 19 | + .enum(['gemini', 'openai', 'ollama']) |
| 20 | + .default('gemini') |
| 21 | + .describe('AI provider to use'), |
| 22 | + }, |
| 23 | + }, |
| 24 | + async ({ filepath, provider }) => { |
78 | 25 | const config: Config = { |
79 | | - aiProvider: provider as 'gemini' | 'openai' | 'ollama', |
| 26 | + aiProvider: provider, |
80 | 27 | geminiApiKey: process.env.GEMINI_API_KEY, |
81 | 28 | openaiApiKey: process.env.OPENAI_API_KEY, |
82 | 29 | }; |
83 | 30 |
|
84 | 31 | try { |
85 | 32 | const result = await extractDocument(filepath, config); |
86 | | - |
87 | 33 | return { |
88 | | - content: [ |
89 | | - { |
90 | | - type: 'text', |
91 | | - text: JSON.stringify(result, null, 2), |
92 | | - }, |
93 | | - ], |
| 34 | + content: [{ type: 'text', text: JSON.stringify(result, null, 2) }], |
94 | 35 | }; |
95 | 36 | } catch (error) { |
96 | 37 | return { |
97 | | - content: [ |
98 | | - { |
99 | | - type: 'text', |
100 | | - text: `Error: ${(error as Error).message}`, |
101 | | - }, |
102 | | - ], |
| 38 | + content: [{ type: 'text', text: `Error: ${(error as Error).message}` }], |
103 | 39 | isError: true, |
104 | 40 | }; |
105 | 41 | } |
106 | 42 | } |
| 43 | +); |
107 | 44 |
|
108 | | - if (request.params.name === 'search_documents') { |
| 45 | +// Register search_documents tool |
| 46 | +server.registerTool( |
| 47 | + 'search_documents', |
| 48 | + { |
| 49 | + description: 'Search indexed documents using natural language', |
| 50 | + inputSchema: { |
| 51 | + query: z.string().describe('Search query in natural language'), |
| 52 | + limit: z.number().default(10).describe('Maximum number of results'), |
| 53 | + }, |
| 54 | + }, |
| 55 | + async () => { |
109 | 56 | return { |
110 | | - content: [ |
111 | | - { |
112 | | - type: 'text', |
113 | | - text: 'Search functionality not yet implemented', |
114 | | - }, |
115 | | - ], |
| 57 | + content: [{ type: 'text', text: 'Search functionality not yet implemented' }], |
116 | 58 | }; |
117 | 59 | } |
118 | | - |
119 | | - throw new Error(`Unknown tool: ${request.params.name}`); |
120 | | -}); |
| 60 | +); |
121 | 61 |
|
122 | 62 | export async function startMCPServer() { |
123 | 63 | const transport = new StdioServerTransport(); |
|
0 commit comments