-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
246 lines (222 loc) · 7.65 KB
/
index.js
File metadata and controls
246 lines (222 loc) · 7.65 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#!/usr/bin/env node
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 dotenv from 'dotenv';
import { ApiClient } from './src/api/client.js';
import logger from './src/logger.js';
import { loadKnowledgeBases, getKnowledgeBases, getKnowledgeBaseById } from './src/config/knowledgeBases.js';
dotenv.config();
const server = new Server(
{
name: 'get-notes-mcp',
version: '1.0.0',
},
{
capabilities: {
tools: {},
},
}
);
/**
* Tool Definitions
*/
const LIST_KNOWLEDGE_BASES_TOOL = {
name: 'list_knowledge_bases',
description: 'List available knowledge bases with their IDs and descriptions. You MUST use this tool first to find the correct kb_id for search_knowledge or recall_knowledge.',
inputSchema: {
type: 'object',
properties: {},
}
};
const SEARCH_KNOWLEDGE_TOOL = {
name: 'search_knowledge',
description: 'Search knowledge base with AI processing. Returns synthesized answers and references.',
inputSchema: {
type: 'object',
properties: {
kb_id: {
type: 'string',
description: 'The ID of the knowledge base to search. Optional if only one KB is configured or to use the default.'
},
question: {
type: 'string',
description: 'The question to ask'
},
deep_seek: {
type: 'boolean',
description: 'Enable deep thinking mode',
default: false
},
history: {
type: 'array',
description: 'Chat history for context',
items: {
type: 'object',
properties: {
content: { type: 'string' },
role: { type: 'string', enum: ['user', 'assistant'] }
}
}
}
},
required: ['question']
}
};
const RECALL_KNOWLEDGE_TOOL = {
name: 'recall_knowledge',
description: 'Raw recall from knowledge base without AI synthesis. Returns list of relevant notes/files.',
inputSchema: {
type: 'object',
properties: {
kb_id: {
type: 'string',
description: 'The ID of the knowledge base to search. Optional if only one KB is configured or to use the default.'
},
question: {
type: 'string',
description: 'The question or query'
},
top_k: {
type: 'number',
description: 'Number of results to return',
default: 10
},
intent_rewrite: {
type: 'boolean',
description: 'Enable intent rewrite',
default: false
}
},
required: ['question']
}
};
server.setRequestHandler(ListToolsRequestSchema, async () => {
return {
tools: [LIST_KNOWLEDGE_BASES_TOOL, SEARCH_KNOWLEDGE_TOOL, RECALL_KNOWLEDGE_TOOL],
};
});
server.setRequestHandler(CallToolRequestSchema, async (request) => {
try {
const { name, arguments: args } = request.params;
switch (name) {
case 'list_knowledge_bases': {
const kbs = getKnowledgeBases();
// Return simplified list (hide sensitive config)
const publicKbs = kbs.map(kb => ({
id: kb.id,
name: kb.name,
description: kb.description
}));
return {
content: [
{
type: 'text',
text: JSON.stringify(publicKbs, null, 2),
},
],
};
}
case 'search_knowledge': {
const { kb_id, ...params } = args;
let kb;
if (kb_id) {
kb = getKnowledgeBaseById(kb_id);
if (!kb) {
throw new Error(`Knowledge base not found: ${kb_id}`);
}
} else {
// Default to the first one
const kbs = getKnowledgeBases();
if (kbs.length === 0) {
throw new Error('No knowledge bases configured');
}
kb = kbs[0];
}
// Initialize client for this specific KB
const client = new ApiClient({
apiKey: kb.config.api_key,
baseUrl: kb.config.api_endpoint
});
// Inject topic_id from config (API requires array format but only supports 1)
if (kb.config.topic_id) {
params.topic_ids = [kb.config.topic_id];
}
// Set default value for deep_seek if not provided
if (params.deep_seek === undefined) {
params.deep_seek = false;
}
const response = await client.searchKnowledge(params);
return {
content: [
{
type: 'text',
text: JSON.stringify(response.data, null, 2),
},
],
};
}
case 'recall_knowledge': {
const { kb_id, ...params } = args;
let kb;
if (kb_id) {
kb = getKnowledgeBaseById(kb_id);
if (!kb) {
throw new Error(`Knowledge base not found: ${kb_id}`);
}
} else {
// Default to the first one
const kbs = getKnowledgeBases();
if (kbs.length === 0) {
throw new Error('No knowledge bases configured');
}
kb = kbs[0];
}
const client = new ApiClient({
apiKey: kb.config.api_key,
baseUrl: kb.config.api_endpoint
});
// Inject topic_id from config
if (kb.config.topic_id) {
params.topic_id = kb.config.topic_id;
}
const response = await client.recallKnowledge(params);
return {
content: [
{
type: 'text',
text: JSON.stringify(response.data, null, 2),
},
],
};
}
default:
throw new Error(`Unknown tool: ${name}`);
}
} catch (error) {
logger.error('Tool execution error', { error: error.message });
return {
content: [
{
type: 'text',
text: `Error: ${error.message}`,
},
],
isError: true,
};
}
});
async function runServer() {
await loadKnowledgeBases();
const transport = new StdioServerTransport();
await server.connect(transport);
logger.info('Get Notes MCP Server running on stdio');
}
runServer().catch((error) => {
logger.error('Fatal error running server', { error });
process.exit(1);
});