claude-mem 3.3.7 → 3.3.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +183 -46
- package/dist/bin/cli.d.ts +2 -0
- package/dist/bin/cli.js +179 -0
- package/dist/commands/compress.d.ts +2 -0
- package/dist/commands/compress.js +27 -0
- package/dist/commands/hooks.d.ts +19 -0
- package/dist/commands/hooks.js +131 -0
- package/dist/commands/install.d.ts +2 -0
- package/dist/commands/install.js +836 -0
- package/dist/commands/load-context.d.ts +2 -0
- package/dist/commands/load-context.js +151 -0
- package/dist/commands/logs.d.ts +2 -0
- package/dist/commands/logs.js +76 -0
- package/dist/commands/migrate-to-jsonl.d.ts +5 -0
- package/dist/commands/migrate-to-jsonl.js +99 -0
- package/dist/commands/restore.d.ts +1 -0
- package/dist/commands/restore.js +20 -0
- package/dist/commands/status.d.ts +1 -0
- package/dist/commands/status.js +136 -0
- package/dist/commands/trash-empty.d.ts +3 -0
- package/dist/commands/trash-empty.js +56 -0
- package/dist/commands/trash-view.d.ts +1 -0
- package/dist/commands/trash-view.js +101 -0
- package/dist/commands/trash.d.ts +6 -0
- package/dist/commands/trash.js +49 -0
- package/dist/commands/uninstall.d.ts +2 -0
- package/dist/commands/uninstall.js +107 -0
- package/dist/constants.d.ts +271 -0
- package/dist/constants.js +199 -0
- package/dist/core/compression/TranscriptCompressor.d.ts +79 -0
- package/dist/core/compression/TranscriptCompressor.js +585 -0
- package/dist/core/orchestration/PromptOrchestrator.d.ts +165 -0
- package/dist/core/orchestration/PromptOrchestrator.js +182 -0
- package/dist/lib/time-utils.d.ts +5 -0
- package/dist/lib/time-utils.js +70 -0
- package/dist/prompts/constants.d.ts +126 -0
- package/dist/prompts/constants.js +161 -0
- package/dist/prompts/index.d.ts +10 -0
- package/dist/prompts/index.js +11 -0
- package/dist/prompts/templates/analysis/AnalysisTemplates.d.ts +13 -0
- package/dist/prompts/templates/analysis/AnalysisTemplates.js +94 -0
- package/dist/prompts/templates/context/ContextTemplates.d.ts +119 -0
- package/dist/prompts/templates/context/ContextTemplates.js +399 -0
- package/dist/prompts/templates/hooks/HookTemplates.d.ts +175 -0
- package/dist/prompts/templates/hooks/HookTemplates.js +394 -0
- package/dist/prompts/templates/hooks/HookTemplates.test.d.ts +7 -0
- package/dist/prompts/templates/hooks/HookTemplates.test.js +127 -0
- package/dist/shared/config.d.ts +4 -0
- package/dist/shared/config.js +41 -0
- package/dist/shared/error-handler.d.ts +22 -0
- package/dist/shared/error-handler.js +142 -0
- package/dist/shared/logger.d.ts +19 -0
- package/dist/shared/logger.js +51 -0
- package/dist/shared/paths.d.ts +28 -0
- package/dist/shared/paths.js +100 -0
- package/dist/shared/settings.d.ts +41 -0
- package/dist/shared/settings.js +81 -0
- package/dist/shared/types.d.ts +145 -0
- package/dist/shared/types.js +78 -0
- package/docs/STATUS.md +155 -0
- package/docs/chroma-backend-migration.md +161 -0
- package/docs/landing-page-outline.md +287 -0
- package/docs/multi-platform-builds.md +96 -0
- package/docs/plans/cloud-service-plan.md +274 -0
- package/docs/plans/fix-response-format-issue.md +61 -0
- package/docs/plans/restructure-session-hook-output.md +102 -0
- package/docs/plans/session-start-hook-investigation.md +45 -0
- package/docs/plans/src-reorganization-plan.md +181 -0
- package/docs/plans/terminal-effects-decision.md +22 -0
- package/docs/plans/terminal-effects-integration.md +82 -0
- package/docs/plans/trash-bin-feature-plan.md +240 -0
- package/docs/plans/trash-bin-minimal-plan.md +102 -0
- package/docs/reference/bun-single-executable.md +584 -0
- package/docs/reference/cc-output-styles.md +99 -0
- package/docs/reference/chroma-mcp-project-memory-example.md +80 -0
- package/docs/reference/chroma-mcp-team-example.md +92 -0
- package/docs/reference/claude-code/cc-hooks.md +787 -0
- package/docs/reference/claude-code/cc-status-line.md +202 -0
- package/docs/reference/claude-code/hook-configuration.md +173 -0
- package/docs/reference/claude-code/hook-responses.md +127 -0
- package/docs/reference/claude-code/hooks.md +175 -0
- package/docs/reference/claude-code/mcp-configuration.md +133 -0
- package/docs/reference/claude-code/session-start-hook.md +82 -0
- package/docs/reference/load-context-format-example.md +33 -0
- package/docs/reference/mcp-sdk/mcp-typescript-sdk-readme.md +1323 -0
- package/docs/reference/mcp-sdk/server-implementation.md +286 -0
- package/docs/reference/mcp-sdk/stdio-transport.md +345 -0
- package/docs/todos/fix-response-format-tasks.md +43 -0
- package/docs/todos/implementation-todos.md +280 -0
- package/docs/todos/restructure-hook-output-tasks.md +103 -0
- package/docs/todos/session-start-hook-fix.md +26 -0
- package/docs/todos/terminal-effects-tasks.md +42 -0
- package/docs/todos/trash-bin-implementation-todos.md +348 -0
- package/docs/todos/trash-bin-minimal-todos.md +27 -0
- package/package.json +56 -6
- package/claude-mem +0 -0
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
# MCP TypeScript SDK Server Implementation Guide
|
|
2
|
+
|
|
3
|
+
## Documentation Source
|
|
4
|
+
- **SDK Version**: @modelcontextprotocol/sdk v1.0.0
|
|
5
|
+
- **Last Verified**: 2025-09-01
|
|
6
|
+
- **Official Docs**: https://github.com/modelcontextprotocol/typescript-sdk
|
|
7
|
+
|
|
8
|
+
## Server Creation Patterns
|
|
9
|
+
|
|
10
|
+
### Low-Level Server vs McpServer
|
|
11
|
+
|
|
12
|
+
The SDK provides two approaches for creating MCP servers:
|
|
13
|
+
|
|
14
|
+
1. **Low-Level Server Class** (Used in claude-mem)
|
|
15
|
+
- Direct control over request handling
|
|
16
|
+
- Manual registration with `setRequestHandler`
|
|
17
|
+
- More flexibility for custom routing logic
|
|
18
|
+
|
|
19
|
+
```typescript
|
|
20
|
+
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
|
21
|
+
|
|
22
|
+
const server = new Server(
|
|
23
|
+
{
|
|
24
|
+
name: 'server-name',
|
|
25
|
+
version: '1.0.0'
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
capabilities: {
|
|
29
|
+
tools: {} // Declare tool capability
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
);
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
2. **High-Level McpServer Class** (Alternative approach)
|
|
36
|
+
- Simplified API with `registerTool`, `registerResource`, `registerPrompt`
|
|
37
|
+
- Automatic routing and validation
|
|
38
|
+
- Less boilerplate code
|
|
39
|
+
|
|
40
|
+
```typescript
|
|
41
|
+
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
42
|
+
|
|
43
|
+
const server = new McpServer({
|
|
44
|
+
name: 'server-name',
|
|
45
|
+
version: '1.0.0'
|
|
46
|
+
});
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Tool Handler Registration
|
|
50
|
+
|
|
51
|
+
### Pattern 1: Single Handler with CallToolRequestSchema (claude-mem approach)
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
import { CallToolRequestSchema } from '@modelcontextprotocol/sdk/types.js';
|
|
55
|
+
|
|
56
|
+
server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
57
|
+
const { name, arguments: args } = request.params;
|
|
58
|
+
|
|
59
|
+
// Validate arguments exist
|
|
60
|
+
if (!args) {
|
|
61
|
+
throw new Error(`No arguments provided for tool: ${name}`);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Route to specific tool implementation
|
|
65
|
+
switch (name) {
|
|
66
|
+
case 'tool-name':
|
|
67
|
+
// Tool implementation
|
|
68
|
+
return {
|
|
69
|
+
content: [{
|
|
70
|
+
type: 'text',
|
|
71
|
+
text: 'Result'
|
|
72
|
+
}]
|
|
73
|
+
};
|
|
74
|
+
default:
|
|
75
|
+
throw new Error(`Unknown tool: ${name}`);
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Pattern 2: Individual Tool Registration (McpServer approach)
|
|
81
|
+
|
|
82
|
+
```typescript
|
|
83
|
+
server.registerTool(
|
|
84
|
+
'tool-name',
|
|
85
|
+
{
|
|
86
|
+
title: 'Tool Title',
|
|
87
|
+
description: 'Tool description',
|
|
88
|
+
inputSchema: { param: z.string() }
|
|
89
|
+
},
|
|
90
|
+
async ({ param }) => ({
|
|
91
|
+
content: [{
|
|
92
|
+
type: 'text',
|
|
93
|
+
text: `Result for ${param}`
|
|
94
|
+
}]
|
|
95
|
+
})
|
|
96
|
+
);
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## Stdio Transport Usage
|
|
100
|
+
|
|
101
|
+
### Standard Pattern for CLI-based Servers
|
|
102
|
+
|
|
103
|
+
```typescript
|
|
104
|
+
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
105
|
+
|
|
106
|
+
async function main() {
|
|
107
|
+
// 1. Initialize backend services first
|
|
108
|
+
await initializeBackend();
|
|
109
|
+
|
|
110
|
+
// 2. Create transport
|
|
111
|
+
const transport = new StdioServerTransport();
|
|
112
|
+
|
|
113
|
+
// 3. Connect server to transport
|
|
114
|
+
await server.connect(transport);
|
|
115
|
+
|
|
116
|
+
// 4. Log to stderr (stdout is for protocol)
|
|
117
|
+
console.error('Server started on stdio');
|
|
118
|
+
}
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### Key Points:
|
|
122
|
+
- **Stdin**: Receives MCP protocol messages
|
|
123
|
+
- **Stdout**: Sends MCP protocol responses
|
|
124
|
+
- **Stderr**: Used for logging and diagnostics
|
|
125
|
+
|
|
126
|
+
## Error Handling Patterns
|
|
127
|
+
|
|
128
|
+
### Tool Error Response
|
|
129
|
+
|
|
130
|
+
```typescript
|
|
131
|
+
try {
|
|
132
|
+
// Tool implementation
|
|
133
|
+
return {
|
|
134
|
+
content: [{
|
|
135
|
+
type: 'text',
|
|
136
|
+
text: 'Success result'
|
|
137
|
+
}]
|
|
138
|
+
};
|
|
139
|
+
} catch (error) {
|
|
140
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
141
|
+
|
|
142
|
+
// Log to stderr for debugging
|
|
143
|
+
console.error(`[Error] Tool: ${name}, Error: ${errorMessage}`);
|
|
144
|
+
|
|
145
|
+
// Return error response with isError flag
|
|
146
|
+
return {
|
|
147
|
+
content: [{
|
|
148
|
+
type: 'text',
|
|
149
|
+
text: `Error: ${errorMessage}`
|
|
150
|
+
}],
|
|
151
|
+
isError: true // Important: Indicates tool failure
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### Startup Error Handling
|
|
157
|
+
|
|
158
|
+
```typescript
|
|
159
|
+
main().catch((error) => {
|
|
160
|
+
console.error('Startup error:', error);
|
|
161
|
+
process.exit(1); // Exit with error code
|
|
162
|
+
});
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
## Response Formatting
|
|
166
|
+
|
|
167
|
+
### Success Response Structure
|
|
168
|
+
|
|
169
|
+
```typescript
|
|
170
|
+
{
|
|
171
|
+
content: [
|
|
172
|
+
{
|
|
173
|
+
type: 'text',
|
|
174
|
+
text: 'Response text'
|
|
175
|
+
}
|
|
176
|
+
]
|
|
177
|
+
}
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
### Error Response Structure
|
|
181
|
+
|
|
182
|
+
```typescript
|
|
183
|
+
{
|
|
184
|
+
content: [
|
|
185
|
+
{
|
|
186
|
+
type: 'text',
|
|
187
|
+
text: 'Error: Description'
|
|
188
|
+
}
|
|
189
|
+
],
|
|
190
|
+
isError: true
|
|
191
|
+
}
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
### Resource Link Response
|
|
195
|
+
|
|
196
|
+
```typescript
|
|
197
|
+
{
|
|
198
|
+
content: [
|
|
199
|
+
{
|
|
200
|
+
type: 'resource_link',
|
|
201
|
+
uri: 'file:///path/to/resource',
|
|
202
|
+
name: 'Resource Name',
|
|
203
|
+
mimeType: 'text/plain',
|
|
204
|
+
description: 'Resource description'
|
|
205
|
+
}
|
|
206
|
+
]
|
|
207
|
+
}
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
## Lifecycle Management
|
|
211
|
+
|
|
212
|
+
### Initialization Pattern
|
|
213
|
+
|
|
214
|
+
```typescript
|
|
215
|
+
async function initializeServer(): Promise<void> {
|
|
216
|
+
try {
|
|
217
|
+
// Initialize backend connections
|
|
218
|
+
await backend.connect();
|
|
219
|
+
console.error('Backend initialized');
|
|
220
|
+
} catch (error) {
|
|
221
|
+
console.error('Initialization failed:', error);
|
|
222
|
+
throw error; // Prevent server startup
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
### Shutdown Pattern
|
|
228
|
+
|
|
229
|
+
```typescript
|
|
230
|
+
function setupShutdownHandlers(): void {
|
|
231
|
+
const handleShutdown = async (signal: string) => {
|
|
232
|
+
console.error(`Received ${signal}, shutting down...`);
|
|
233
|
+
|
|
234
|
+
try {
|
|
235
|
+
await backend.disconnect();
|
|
236
|
+
process.exit(0); // Clean exit
|
|
237
|
+
} catch (error) {
|
|
238
|
+
console.error('Shutdown error:', error);
|
|
239
|
+
process.exit(1); // Error exit
|
|
240
|
+
}
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
process.on('SIGINT', () => handleShutdown('SIGINT'));
|
|
244
|
+
process.on('SIGTERM', () => handleShutdown('SIGTERM'));
|
|
245
|
+
|
|
246
|
+
// Handle unexpected errors
|
|
247
|
+
process.on('uncaughtException', async (error) => {
|
|
248
|
+
console.error('Uncaught exception:', error);
|
|
249
|
+
await backend.disconnect();
|
|
250
|
+
process.exit(1);
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
## Best Practices Summary
|
|
256
|
+
|
|
257
|
+
1. **Server Creation**
|
|
258
|
+
- Use low-level Server for custom routing
|
|
259
|
+
- Use McpServer for standard implementations
|
|
260
|
+
|
|
261
|
+
2. **Transport Usage**
|
|
262
|
+
- Initialize backends before connecting transport
|
|
263
|
+
- Use StdioServerTransport for CLI tools
|
|
264
|
+
- Log to stderr, not stdout
|
|
265
|
+
|
|
266
|
+
3. **Error Handling**
|
|
267
|
+
- Always validate tool arguments
|
|
268
|
+
- Include isError flag in error responses
|
|
269
|
+
- Log errors to stderr with context
|
|
270
|
+
|
|
271
|
+
4. **Response Format**
|
|
272
|
+
- Always return content array
|
|
273
|
+
- Use consistent type/text structure
|
|
274
|
+
- Include isError for failures
|
|
275
|
+
|
|
276
|
+
5. **Lifecycle**
|
|
277
|
+
- Clean initialization sequence
|
|
278
|
+
- Graceful shutdown handlers
|
|
279
|
+
- Proper exit codes (0 for success, 1 for error)
|
|
280
|
+
|
|
281
|
+
## References
|
|
282
|
+
|
|
283
|
+
- [MCP TypeScript SDK README](https://github.com/modelcontextprotocol/typescript-sdk)
|
|
284
|
+
- [Low-Level Server Pattern](https://github.com/modelcontextprotocol/typescript-sdk#low-level-server-implementation)
|
|
285
|
+
- [Stdio Transport Example](https://github.com/modelcontextprotocol/typescript-sdk#stdio-transport)
|
|
286
|
+
- [Error Handling Examples](https://github.com/modelcontextprotocol/typescript-sdk#sqlite-explorer)
|
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
# MCP TypeScript SDK Stdio Transport Guide
|
|
2
|
+
|
|
3
|
+
## Documentation Source
|
|
4
|
+
- **SDK Version**: @modelcontextprotocol/sdk v1.0.0
|
|
5
|
+
- **Last Verified**: 2025-09-01
|
|
6
|
+
- **Official Docs**: https://github.com/modelcontextprotocol/typescript-sdk
|
|
7
|
+
|
|
8
|
+
## Stdio Transport Overview
|
|
9
|
+
|
|
10
|
+
The StdioServerTransport enables MCP servers to communicate via standard input/output streams, making them ideal for CLI tools and direct integrations with Claude Code.
|
|
11
|
+
|
|
12
|
+
## Communication Channels
|
|
13
|
+
|
|
14
|
+
### Stream Usage
|
|
15
|
+
- **stdin**: Receives MCP protocol messages (JSON-RPC)
|
|
16
|
+
- **stdout**: Sends MCP protocol responses (JSON-RPC)
|
|
17
|
+
- **stderr**: Logging and diagnostic output
|
|
18
|
+
|
|
19
|
+
### Important Rules
|
|
20
|
+
1. **Never write non-protocol data to stdout** - This will break the protocol
|
|
21
|
+
2. **Always use console.error() for logging** - Goes to stderr
|
|
22
|
+
3. **Handle binary data carefully** - Protocol is text-based JSON
|
|
23
|
+
|
|
24
|
+
## Implementation Patterns
|
|
25
|
+
|
|
26
|
+
### Basic Stdio Server Setup
|
|
27
|
+
|
|
28
|
+
```typescript
|
|
29
|
+
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
|
30
|
+
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
31
|
+
|
|
32
|
+
// Create server
|
|
33
|
+
const server = new Server(
|
|
34
|
+
{ name: 'my-server', version: '1.0.0' },
|
|
35
|
+
{ capabilities: { tools: {} } }
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
// Create and connect transport
|
|
39
|
+
const transport = new StdioServerTransport();
|
|
40
|
+
await server.connect(transport);
|
|
41
|
+
|
|
42
|
+
// Server is now listening on stdin/stdout
|
|
43
|
+
console.error('Server started'); // Note: console.error for logging
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### With McpServer (High-Level API)
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
50
|
+
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
51
|
+
|
|
52
|
+
const server = new McpServer({
|
|
53
|
+
name: 'my-server',
|
|
54
|
+
version: '1.0.0'
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
// Register tools, resources, prompts...
|
|
58
|
+
server.registerTool(...);
|
|
59
|
+
|
|
60
|
+
// Connect to stdio
|
|
61
|
+
const transport = new StdioServerTransport();
|
|
62
|
+
await server.connect(transport);
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## CLI Entry Point Pattern
|
|
66
|
+
|
|
67
|
+
### Proper Module Detection (ES Modules)
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
#!/usr/bin/env node
|
|
71
|
+
|
|
72
|
+
import { fileURLToPath } from 'url';
|
|
73
|
+
import { dirname } from 'path';
|
|
74
|
+
|
|
75
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
76
|
+
const __dirname = dirname(__filename);
|
|
77
|
+
|
|
78
|
+
// Only run if executed directly
|
|
79
|
+
if (process.argv[1] === __filename ||
|
|
80
|
+
process.argv[1].endsWith('server.js')) {
|
|
81
|
+
main().catch((error) => {
|
|
82
|
+
console.error('Startup error:', error);
|
|
83
|
+
process.exit(1);
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### Main Function Pattern
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
91
|
+
async function main(): Promise<void> {
|
|
92
|
+
try {
|
|
93
|
+
// 1. Initialize dependencies
|
|
94
|
+
await initializeDatabase();
|
|
95
|
+
|
|
96
|
+
// 2. Create server
|
|
97
|
+
const server = createServer();
|
|
98
|
+
|
|
99
|
+
// 3. Create transport
|
|
100
|
+
const transport = new StdioServerTransport();
|
|
101
|
+
|
|
102
|
+
// 4. Connect
|
|
103
|
+
await server.connect(transport);
|
|
104
|
+
|
|
105
|
+
// 5. Setup shutdown handlers
|
|
106
|
+
setupShutdownHandlers();
|
|
107
|
+
|
|
108
|
+
// 6. Log readiness to stderr
|
|
109
|
+
console.error('MCP server ready on stdio');
|
|
110
|
+
|
|
111
|
+
} catch (error) {
|
|
112
|
+
console.error('Failed to start server:', error);
|
|
113
|
+
process.exit(1);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## Shutdown Handling
|
|
119
|
+
|
|
120
|
+
### Graceful Shutdown Pattern
|
|
121
|
+
|
|
122
|
+
```typescript
|
|
123
|
+
function setupShutdownHandlers(): void {
|
|
124
|
+
const shutdown = async (signal: string) => {
|
|
125
|
+
console.error(`\nReceived ${signal}, shutting down...`);
|
|
126
|
+
|
|
127
|
+
try {
|
|
128
|
+
// Clean up resources
|
|
129
|
+
await cleanupResources();
|
|
130
|
+
|
|
131
|
+
// Note: Transport cleanup is handled automatically
|
|
132
|
+
process.exit(0);
|
|
133
|
+
} catch (error) {
|
|
134
|
+
console.error('Shutdown error:', error);
|
|
135
|
+
process.exit(1);
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
// Handle termination signals
|
|
140
|
+
process.on('SIGINT', () => shutdown('SIGINT')); // Ctrl+C
|
|
141
|
+
process.on('SIGTERM', () => shutdown('SIGTERM')); // Kill
|
|
142
|
+
process.on('SIGHUP', () => shutdown('SIGHUP')); // Terminal closed
|
|
143
|
+
|
|
144
|
+
// Handle errors
|
|
145
|
+
process.on('uncaughtException', (error) => {
|
|
146
|
+
console.error('Uncaught exception:', error);
|
|
147
|
+
process.exit(1);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
process.on('unhandledRejection', (reason) => {
|
|
151
|
+
console.error('Unhandled rejection:', reason);
|
|
152
|
+
process.exit(1);
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
## Logging Best Practices
|
|
158
|
+
|
|
159
|
+
### Do's and Don'ts
|
|
160
|
+
|
|
161
|
+
```typescript
|
|
162
|
+
// ✅ DO: Log to stderr
|
|
163
|
+
console.error('Server initialized');
|
|
164
|
+
console.error('Processing request:', requestId);
|
|
165
|
+
console.error('Debug info:', { data });
|
|
166
|
+
|
|
167
|
+
// ❌ DON'T: Log to stdout
|
|
168
|
+
console.log('This breaks the protocol!'); // NEVER DO THIS
|
|
169
|
+
|
|
170
|
+
// ✅ DO: Use structured logging to stderr
|
|
171
|
+
const log = (level: string, message: string, data?: any) => {
|
|
172
|
+
console.error(JSON.stringify({
|
|
173
|
+
timestamp: new Date().toISOString(),
|
|
174
|
+
level,
|
|
175
|
+
message,
|
|
176
|
+
...data
|
|
177
|
+
}));
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
log('info', 'Server started', { port: 'stdio' });
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
### Debug Mode Pattern
|
|
184
|
+
|
|
185
|
+
```typescript
|
|
186
|
+
const DEBUG = process.env.DEBUG === 'true';
|
|
187
|
+
|
|
188
|
+
const debug = (message: string, ...args: any[]) => {
|
|
189
|
+
if (DEBUG) {
|
|
190
|
+
console.error(`[DEBUG] ${message}`, ...args);
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
// Usage
|
|
195
|
+
debug('Request received:', request);
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
## Testing Stdio Servers
|
|
199
|
+
|
|
200
|
+
### Manual Testing
|
|
201
|
+
|
|
202
|
+
```bash
|
|
203
|
+
# Start server and interact manually
|
|
204
|
+
node dist/server.js
|
|
205
|
+
|
|
206
|
+
# With debug logging
|
|
207
|
+
DEBUG=true node dist/server.js
|
|
208
|
+
|
|
209
|
+
# Pipe test input
|
|
210
|
+
echo '{"jsonrpc":"2.0","method":"initialize","params":{},"id":1}' | node dist/server.js
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
### Automated Testing Pattern
|
|
214
|
+
|
|
215
|
+
```typescript
|
|
216
|
+
import { spawn } from 'child_process';
|
|
217
|
+
|
|
218
|
+
function testServer() {
|
|
219
|
+
const server = spawn('node', ['dist/server.js']);
|
|
220
|
+
|
|
221
|
+
// Capture stderr for logs
|
|
222
|
+
server.stderr.on('data', (data) => {
|
|
223
|
+
console.log('Server log:', data.toString());
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
// Capture stdout for protocol
|
|
227
|
+
let response = '';
|
|
228
|
+
server.stdout.on('data', (data) => {
|
|
229
|
+
response += data.toString();
|
|
230
|
+
// Parse and validate response
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
// Send test request
|
|
234
|
+
server.stdin.write(JSON.stringify({
|
|
235
|
+
jsonrpc: '2.0',
|
|
236
|
+
method: 'initialize',
|
|
237
|
+
params: {},
|
|
238
|
+
id: 1
|
|
239
|
+
}));
|
|
240
|
+
server.stdin.end();
|
|
241
|
+
}
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
## Common Issues and Solutions
|
|
245
|
+
|
|
246
|
+
### Issue 1: Protocol Corruption
|
|
247
|
+
|
|
248
|
+
**Problem**: Random text in stdout breaks communication
|
|
249
|
+
**Solution**: Always use console.error() for logging
|
|
250
|
+
|
|
251
|
+
```typescript
|
|
252
|
+
// Wrong
|
|
253
|
+
console.log('Debug:', data); // Breaks protocol
|
|
254
|
+
|
|
255
|
+
// Right
|
|
256
|
+
console.error('Debug:', data); // Safe for debugging
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
### Issue 2: Server Not Responding
|
|
260
|
+
|
|
261
|
+
**Problem**: Server starts but doesn't respond to requests
|
|
262
|
+
**Solution**: Ensure transport is connected
|
|
263
|
+
|
|
264
|
+
```typescript
|
|
265
|
+
// Check connection is awaited
|
|
266
|
+
await server.connect(transport); // Must await!
|
|
267
|
+
console.error('Transport connected');
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
### Issue 3: Premature Exit
|
|
271
|
+
|
|
272
|
+
**Problem**: Server exits immediately
|
|
273
|
+
**Solution**: Don't close stdin/stdout
|
|
274
|
+
|
|
275
|
+
```typescript
|
|
276
|
+
// Wrong
|
|
277
|
+
process.stdin.end(); // Don't do this
|
|
278
|
+
|
|
279
|
+
// Right
|
|
280
|
+
// Let the transport manage streams
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
## Integration with Claude Code
|
|
284
|
+
|
|
285
|
+
### Configuration in .claude.json
|
|
286
|
+
|
|
287
|
+
```json
|
|
288
|
+
{
|
|
289
|
+
"mcpServers": {
|
|
290
|
+
"my-server": {
|
|
291
|
+
"command": "node",
|
|
292
|
+
"args": ["/path/to/dist/server.js"],
|
|
293
|
+
"env": {
|
|
294
|
+
"DEBUG": "false"
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
### Best Practices for Claude Code Integration
|
|
302
|
+
|
|
303
|
+
1. **Startup Messages**: Log clear startup messages to stderr
|
|
304
|
+
2. **Error Messages**: Provide actionable error messages
|
|
305
|
+
3. **Ready Signal**: Log when server is ready to accept requests
|
|
306
|
+
4. **Version Info**: Include version in startup logs
|
|
307
|
+
|
|
308
|
+
```typescript
|
|
309
|
+
console.error(`Starting ${serverName} v${version}`);
|
|
310
|
+
console.error('Initializing...');
|
|
311
|
+
// ... initialization ...
|
|
312
|
+
console.error(`${serverName} ready on stdio`);
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
## Performance Considerations
|
|
316
|
+
|
|
317
|
+
### Buffering and Streaming
|
|
318
|
+
|
|
319
|
+
```typescript
|
|
320
|
+
// For large responses, consider streaming
|
|
321
|
+
import { Transform } from 'stream';
|
|
322
|
+
|
|
323
|
+
class ResponseStream extends Transform {
|
|
324
|
+
_transform(chunk: any, encoding: string, callback: Function) {
|
|
325
|
+
// Process chunk
|
|
326
|
+
this.push(JSON.stringify(chunk));
|
|
327
|
+
callback();
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
```
|
|
331
|
+
|
|
332
|
+
### Memory Management
|
|
333
|
+
|
|
334
|
+
```typescript
|
|
335
|
+
// Clear large objects after use
|
|
336
|
+
let largeData = await processData();
|
|
337
|
+
// Use data...
|
|
338
|
+
largeData = null; // Allow GC
|
|
339
|
+
```
|
|
340
|
+
|
|
341
|
+
## References
|
|
342
|
+
|
|
343
|
+
- [StdioServerTransport Docs](https://github.com/modelcontextprotocol/typescript-sdk#stdio-transport)
|
|
344
|
+
- [Server Examples](https://github.com/modelcontextprotocol/typescript-sdk/tree/main/src/examples/server)
|
|
345
|
+
- [MCP Protocol Specification](https://modelcontextprotocol.io/docs)
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# Todo: Fix Response Format Issue
|
|
2
|
+
|
|
3
|
+
## Immediate Tasks
|
|
4
|
+
|
|
5
|
+
### 1. Simplify Analysis Prompt
|
|
6
|
+
- [ ] Open `src/prompts/templates/analysis/AnalysisTemplates.ts`
|
|
7
|
+
- [ ] Remove MCP tool instructions from `buildCompleteAnalysisPrompt`
|
|
8
|
+
- [ ] Remove the entity extraction section
|
|
9
|
+
- [ ] Focus prompt solely on generating pipe-separated summaries
|
|
10
|
+
- [ ] Ensure examples clearly show the 5-part format
|
|
11
|
+
|
|
12
|
+
### 2. Fix Parser Validation
|
|
13
|
+
- [ ] Open `src/core/compression/TranscriptCompressor.ts`
|
|
14
|
+
- [ ] Update `extractPipeSeparatedLines` to validate exactly 5 parts
|
|
15
|
+
- [ ] Change condition from `>= 3` to `=== 5`
|
|
16
|
+
- [ ] Add debug logging for rejected lines
|
|
17
|
+
|
|
18
|
+
### 3. Add Better Error Handling
|
|
19
|
+
- [ ] Log the actual format when parsing fails
|
|
20
|
+
- [ ] Show first few lines of response in error messages
|
|
21
|
+
- [ ] Add format validation feedback
|
|
22
|
+
|
|
23
|
+
### 4. Test Changes
|
|
24
|
+
- [ ] Run `npm run build` to verify no TypeScript errors
|
|
25
|
+
- [ ] Test compression on a sample transcript
|
|
26
|
+
- [ ] Verify pipe-separated output is generated
|
|
27
|
+
- [ ] Check that summaries are extracted to index
|
|
28
|
+
|
|
29
|
+
### 5. Verify Fix
|
|
30
|
+
- [ ] Run compression on previous failed sessions if possible
|
|
31
|
+
- [ ] Confirm summaries are now extracted
|
|
32
|
+
- [ ] Check archive files contain summaries
|
|
33
|
+
- [ ] Verify no "NO SUMMARIES EXTRACTED" errors
|
|
34
|
+
|
|
35
|
+
## Files to Modify
|
|
36
|
+
1. `src/prompts/templates/analysis/AnalysisTemplates.ts` - Simplify prompt
|
|
37
|
+
2. `src/core/compression/TranscriptCompressor.ts` - Fix parser validation
|
|
38
|
+
3. `src/shared/logger.ts` - Add better error context (optional)
|
|
39
|
+
|
|
40
|
+
## Expected Outcome
|
|
41
|
+
- Claude returns clean pipe-separated lines
|
|
42
|
+
- Parser successfully extracts 3-10 summaries
|
|
43
|
+
- Sessions are properly indexed for future reference
|