forked from codeaholicguy/ai-devkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
205 lines (190 loc) · 7.09 KB
/
server.ts
File metadata and controls
205 lines (190 loc) · 7.09 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
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 { storeKnowledge } from './handlers/store';
import { searchKnowledge } from './handlers/search';
import { updateKnowledge } from './handlers/update';
import { KnowledgeMemoryError } from './utils/errors';
import type { StoreKnowledgeInput, SearchKnowledgeInput, UpdateKnowledgeInput } from './types';
const SERVER_NAME = 'ai-devkit-memory';
const SERVER_VERSION = '0.1.0';
const STORE_TOOL = {
name: 'memory_storeKnowledge',
description: 'Store a new knowledge item. Use this to save actionable guidelines, rules, or patterns for future reference.',
inputSchema: {
type: 'object' as const,
properties: {
title: {
type: 'string',
description: 'Short, explicit description of the rule (5-12 words, 10-100 chars)',
},
content: {
type: 'string',
description: 'Detailed explanation in markdown format. Supports code blocks and examples. (50-5000 chars)',
},
tags: {
type: 'array',
items: { type: 'string' },
description: 'Optional domain keywords (e.g., ["api", "backend"]). Max 10 tags.',
},
scope: {
type: 'string',
description: 'Optional scope: "global", "project:<name>", or "repo:<name>". Default: "global"',
},
},
required: ['title', 'content'],
},
};
const UPDATE_TOOL = {
name: 'memory_updateKnowledge',
description: 'Update an existing knowledge item by ID. Use this to correct outdated or inaccurate knowledge.',
inputSchema: {
type: 'object' as const,
properties: {
id: {
type: 'string',
description: 'UUID of the knowledge item to update',
},
title: {
type: 'string',
description: 'New title (10-100 chars). Only provide if changing.',
},
content: {
type: 'string',
description: 'New content in markdown format (50-5000 chars). Only provide if changing.',
},
tags: {
type: 'array',
items: { type: 'string' },
description: 'New tags (replaces existing). Only provide if changing. Max 10 tags.',
},
scope: {
type: 'string',
description: 'New scope: "global", "project:<name>", or "repo:<name>". Only provide if changing.',
},
},
required: ['id'],
},
};
const SEARCH_TOOL = {
name: 'memory_searchKnowledge',
description: 'Search for relevant knowledge based on a task description. Returns ranked results.',
inputSchema: {
type: 'object' as const,
properties: {
query: {
type: 'string',
description: 'Natural language task description to search for relevant knowledge (3-500 chars)',
},
contextTags: {
type: 'array',
items: { type: 'string' },
description: 'Optional tags to boost matching results (e.g., ["api", "backend"])',
},
scope: {
type: 'string',
description: 'Optional project/repo scope filter. Results from this scope are prioritized.',
},
limit: {
type: 'number',
description: 'Maximum number of results to return (1-20, default: 5)',
},
},
required: ['query'],
},
};
export const TOOLS = [STORE_TOOL, UPDATE_TOOL, SEARCH_TOOL];
export function createServer(): Server {
const server = new Server(
{
name: SERVER_NAME,
version: SERVER_VERSION,
},
{
capabilities: {
tools: {},
},
}
);
// List available tools
server.setRequestHandler(ListToolsRequestSchema, async () => {
return {
tools: [STORE_TOOL, UPDATE_TOOL, SEARCH_TOOL],
};
});
// Handle tool calls
server.setRequestHandler(CallToolRequestSchema, async (request) => {
const { name, arguments: args } = request.params;
try {
// Backward-compat: accept deprecated dotted names so agents with
// stale prompts/templates continue to work. Remove in next major.
if (name === 'memory_storeKnowledge' || name === 'memory.storeKnowledge') {
const input = args as unknown as StoreKnowledgeInput;
const result = storeKnowledge(input);
return {
content: [
{
type: 'text' as const,
text: JSON.stringify(result, null, 2),
},
],
};
}
if (name === 'memory_updateKnowledge' || name === 'memory.updateKnowledge') {
const input = args as unknown as UpdateKnowledgeInput;
const result = updateKnowledge(input);
return {
content: [
{
type: 'text' as const,
text: JSON.stringify(result, null, 2),
},
],
};
}
if (name === 'memory_searchKnowledge' || name === 'memory.searchKnowledge') {
const input = args as unknown as SearchKnowledgeInput;
const result = searchKnowledge(input);
return {
content: [
{
type: 'text' as const,
text: JSON.stringify(result, null, 2),
},
],
};
}
return {
content: [
{
type: 'text' as const,
text: JSON.stringify({ error: 'UNKNOWN_TOOL', message: `Unknown tool: ${name}` }),
},
],
isError: true,
};
} catch (error) {
const errorResponse = error instanceof KnowledgeMemoryError
? error.toJSON()
: { error: 'INTERNAL_ERROR', message: error instanceof Error ? error.message : String(error) };
return {
content: [
{
type: 'text' as const,
text: JSON.stringify(errorResponse, null, 2),
},
],
isError: true,
};
}
});
return server;
}
export async function runServer(): Promise<void> {
const server = createServer();
const transport = new StdioServerTransport();
await server.connect(transport);
}