@synth-coder/memhub 0.1.3 → 0.1.5
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 +2 -2
- package/README.zh-CN.md +1 -1
- package/dist/src/contracts/mcp.d.ts +2 -112
- package/dist/src/contracts/mcp.d.ts.map +1 -1
- package/dist/src/contracts/mcp.js +8 -30
- package/dist/src/contracts/mcp.js.map +1 -1
- package/dist/src/server/mcp-server.d.ts +4 -74
- package/dist/src/server/mcp-server.d.ts.map +1 -1
- package/dist/src/server/mcp-server.js +55 -209
- package/dist/src/server/mcp-server.js.map +1 -1
- package/docs/proposals/mcp-typescript-sdk-refactor.md +568 -0
- package/package.json +4 -3
- package/src/contracts/mcp.ts +10 -145
- package/src/server/mcp-server.ts +66 -274
- package/test/server/mcp-server.test.ts +78 -8
- package/test/server/mcp-server-internals.test.ts +0 -257
package/README.md
CHANGED
|
@@ -72,7 +72,7 @@ Example MCP client config:
|
|
|
72
72
|
"command": "npx",
|
|
73
73
|
"args": ["-y", "@synth-coder/memhub"],
|
|
74
74
|
"env": {
|
|
75
|
-
"MEMHUB_STORAGE_PATH": "/absolute/path/to
|
|
75
|
+
"MEMHUB_STORAGE_PATH": "/absolute/path/to/.memhub",
|
|
76
76
|
"MEMHUB_LOG_LEVEL": "info"
|
|
77
77
|
}
|
|
78
78
|
}
|
|
@@ -97,7 +97,7 @@ Example MCP client config:
|
|
|
97
97
|
|
|
98
98
|
## Environment Variables
|
|
99
99
|
|
|
100
|
-
- `MEMHUB_STORAGE_PATH` (default:
|
|
100
|
+
- `MEMHUB_STORAGE_PATH` (default: `~/.memhub`)
|
|
101
101
|
- `MEMHUB_LOG_LEVEL` (default: `info`, options: `debug|info|warn|error`)
|
|
102
102
|
|
|
103
103
|
---
|
package/README.zh-CN.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* MCP (Model Context Protocol) specific types and constants
|
|
3
|
-
*
|
|
3
|
+
* Tool definitions and business-level types (protocol types provided by SDK)
|
|
4
4
|
*/
|
|
5
5
|
import type { Memory } from './types.js';
|
|
6
6
|
/** Current MCP protocol version */
|
|
@@ -10,74 +10,6 @@ export declare const SERVER_INFO: {
|
|
|
10
10
|
readonly name: "memhub";
|
|
11
11
|
readonly version: "0.1.0";
|
|
12
12
|
};
|
|
13
|
-
/** JSON-RPC request ID */
|
|
14
|
-
export type RequestId = string | number;
|
|
15
|
-
/** Base JSON-RPC request structure */
|
|
16
|
-
export interface JsonRpcRequest<T = unknown> {
|
|
17
|
-
jsonrpc: '2.0';
|
|
18
|
-
id?: RequestId;
|
|
19
|
-
method: string;
|
|
20
|
-
params?: T;
|
|
21
|
-
}
|
|
22
|
-
/** Base JSON-RPC response structure */
|
|
23
|
-
export interface JsonRpcResponse<T = unknown> {
|
|
24
|
-
jsonrpc: '2.0';
|
|
25
|
-
id: RequestId | null;
|
|
26
|
-
result?: T;
|
|
27
|
-
error?: JsonRpcError;
|
|
28
|
-
}
|
|
29
|
-
/** JSON-RPC error structure */
|
|
30
|
-
export interface JsonRpcError {
|
|
31
|
-
code: number;
|
|
32
|
-
message: string;
|
|
33
|
-
data?: unknown;
|
|
34
|
-
}
|
|
35
|
-
/** JSON-RPC notification (no response expected) */
|
|
36
|
-
export interface JsonRpcNotification<T = unknown> {
|
|
37
|
-
jsonrpc: '2.0';
|
|
38
|
-
method: string;
|
|
39
|
-
params?: T;
|
|
40
|
-
}
|
|
41
|
-
/** Initialize request parameters */
|
|
42
|
-
export interface InitializeParams {
|
|
43
|
-
protocolVersion: string;
|
|
44
|
-
capabilities: ClientCapabilities;
|
|
45
|
-
clientInfo: Implementation;
|
|
46
|
-
}
|
|
47
|
-
/** Initialize result */
|
|
48
|
-
export interface InitializeResult {
|
|
49
|
-
protocolVersion: string;
|
|
50
|
-
capabilities: ServerCapabilities;
|
|
51
|
-
serverInfo: Implementation;
|
|
52
|
-
}
|
|
53
|
-
/** Implementation information */
|
|
54
|
-
export interface Implementation {
|
|
55
|
-
name: string;
|
|
56
|
-
version: string;
|
|
57
|
-
}
|
|
58
|
-
/** Client capabilities */
|
|
59
|
-
export interface ClientCapabilities {
|
|
60
|
-
readonly experimental?: Record<string, unknown>;
|
|
61
|
-
readonly roots?: {
|
|
62
|
-
listChanged?: boolean;
|
|
63
|
-
};
|
|
64
|
-
readonly sampling?: Record<string, unknown>;
|
|
65
|
-
}
|
|
66
|
-
/** Server capabilities */
|
|
67
|
-
export interface ServerCapabilities {
|
|
68
|
-
readonly experimental?: Record<string, unknown>;
|
|
69
|
-
readonly logging?: Record<string, unknown>;
|
|
70
|
-
readonly prompts?: {
|
|
71
|
-
listChanged?: boolean;
|
|
72
|
-
};
|
|
73
|
-
readonly resources?: {
|
|
74
|
-
subscribe?: boolean;
|
|
75
|
-
listChanged?: boolean;
|
|
76
|
-
};
|
|
77
|
-
readonly tools?: {
|
|
78
|
-
listChanged?: boolean;
|
|
79
|
-
};
|
|
80
|
-
}
|
|
81
13
|
/** Tool definition for tool/list */
|
|
82
14
|
export interface Tool {
|
|
83
15
|
name: string;
|
|
@@ -91,54 +23,12 @@ export interface ToolInputSchema {
|
|
|
91
23
|
required?: string[];
|
|
92
24
|
additionalProperties?: boolean;
|
|
93
25
|
}
|
|
94
|
-
/** Tool call request */
|
|
95
|
-
export interface ToolCallRequest {
|
|
96
|
-
name: string;
|
|
97
|
-
arguments?: Record<string, unknown>;
|
|
98
|
-
}
|
|
99
|
-
/** Tool call result */
|
|
100
|
-
export interface ToolCallResult {
|
|
101
|
-
content: ToolContent[];
|
|
102
|
-
isError?: boolean;
|
|
103
|
-
}
|
|
104
|
-
/** Tool content types */
|
|
105
|
-
export type ToolContent = TextContent | ImageContent;
|
|
106
|
-
/** Text content */
|
|
107
|
-
export interface TextContent {
|
|
108
|
-
type: 'text';
|
|
109
|
-
text: string;
|
|
110
|
-
}
|
|
111
|
-
/** Image content */
|
|
112
|
-
export interface ImageContent {
|
|
113
|
-
type: 'image';
|
|
114
|
-
data: string;
|
|
115
|
-
mimeType: string;
|
|
116
|
-
}
|
|
117
26
|
/** All available tool names */
|
|
118
27
|
export declare const TOOL_NAMES: readonly ["memory_load", "memory_update"];
|
|
119
28
|
/** Tool name type */
|
|
120
29
|
export type ToolName = (typeof TOOL_NAMES)[number];
|
|
121
30
|
/** Tool definitions for MCP server */
|
|
122
31
|
export declare const TOOL_DEFINITIONS: readonly Tool[];
|
|
123
|
-
/** All MCP method names */
|
|
124
|
-
export declare const MCP_METHODS: {
|
|
125
|
-
readonly INITIALIZE: "initialize";
|
|
126
|
-
readonly INITIALIZED: "notifications/initialized";
|
|
127
|
-
readonly SHUTDOWN: "shutdown";
|
|
128
|
-
readonly EXIT: "exit";
|
|
129
|
-
readonly TOOLS_LIST: "tools/list";
|
|
130
|
-
readonly TOOLS_CALL: "tools/call";
|
|
131
|
-
readonly LOGGING_MESSAGE: "notifications/message";
|
|
132
|
-
readonly PROGRESS: "notifications/progress";
|
|
133
|
-
};
|
|
134
|
-
/** Standard JSON-RPC error codes */
|
|
135
|
-
export declare const JSONRPC_ERROR_CODES: {
|
|
136
|
-
readonly PARSE_ERROR: -32700;
|
|
137
|
-
readonly INVALID_REQUEST: -32600;
|
|
138
|
-
readonly METHOD_NOT_FOUND: -32601;
|
|
139
|
-
readonly INVALID_PARAMS: -32602;
|
|
140
|
-
readonly INTERNAL_ERROR: -32603;
|
|
141
|
-
};
|
|
142
32
|
/** MemHub custom error codes */
|
|
143
33
|
export declare const MEMHUB_ERROR_CODES: {
|
|
144
34
|
readonly NOT_FOUND: -32001;
|
|
@@ -146,7 +36,7 @@ export declare const MEMHUB_ERROR_CODES: {
|
|
|
146
36
|
readonly VALIDATION_ERROR: -32003;
|
|
147
37
|
readonly DUPLICATE_ERROR: -32004;
|
|
148
38
|
};
|
|
149
|
-
/** Combined error codes */
|
|
39
|
+
/** Combined error codes (includes JSON-RPC standard codes) */
|
|
150
40
|
export declare const ERROR_CODES: {
|
|
151
41
|
readonly NOT_FOUND: -32001;
|
|
152
42
|
readonly STORAGE_ERROR: -32002;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mcp.d.ts","sourceRoot":"","sources":["../../../src/contracts/mcp.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AAMzC,mCAAmC;AACnC,eAAO,MAAM,oBAAoB,eAAe,CAAC;AAEjD,yBAAyB;AACzB,eAAO,MAAM,WAAW;;;CAGd,CAAC;AAMX,
|
|
1
|
+
{"version":3,"file":"mcp.d.ts","sourceRoot":"","sources":["../../../src/contracts/mcp.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AAMzC,mCAAmC;AACnC,eAAO,MAAM,oBAAoB,eAAe,CAAC;AAEjD,yBAAyB;AACzB,eAAO,MAAM,WAAW;;;CAGd,CAAC;AAMX,oCAAoC;AACpC,MAAM,WAAW,IAAI;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,WAAW,EAAE,eAAe,CAAC;CAC9B;AAED,sCAAsC;AACtC,MAAM,WAAW,eAAe;IAC9B,IAAI,EAAE,QAAQ,CAAC;IACf,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACrC,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;IACpB,oBAAoB,CAAC,EAAE,OAAO,CAAC;CAChC;AAMD,+BAA+B;AAC/B,eAAO,MAAM,UAAU,2CAA4C,CAAC;AAEpE,qBAAqB;AACrB,MAAM,MAAM,QAAQ,GAAG,CAAC,OAAO,UAAU,CAAC,CAAC,MAAM,CAAC,CAAC;AAEnD,sCAAsC;AACtC,eAAO,MAAM,gBAAgB,EAAE,SAAS,IAAI,EAmDlC,CAAC;AAMX,gCAAgC;AAChC,eAAO,MAAM,kBAAkB;;;;;CAKrB,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AAC,EAAE,MAAM,CAAC;IAClB,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB,GACD,KAAK,CAAC"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* MCP (Model Context Protocol) specific types and constants
|
|
3
|
-
*
|
|
3
|
+
* Tool definitions and business-level types (protocol types provided by SDK)
|
|
4
4
|
*/
|
|
5
5
|
// ============================================================================
|
|
6
6
|
// MCP Protocol Version
|
|
@@ -69,34 +69,8 @@ export const TOOL_DEFINITIONS = [
|
|
|
69
69
|
},
|
|
70
70
|
];
|
|
71
71
|
// ============================================================================
|
|
72
|
-
//
|
|
72
|
+
// Error Codes (MemHub Custom)
|
|
73
73
|
// ============================================================================
|
|
74
|
-
/** All MCP method names */
|
|
75
|
-
export const MCP_METHODS = {
|
|
76
|
-
// Lifecycle
|
|
77
|
-
INITIALIZE: 'initialize',
|
|
78
|
-
INITIALIZED: 'notifications/initialized',
|
|
79
|
-
SHUTDOWN: 'shutdown',
|
|
80
|
-
EXIT: 'exit',
|
|
81
|
-
// Tools
|
|
82
|
-
TOOLS_LIST: 'tools/list',
|
|
83
|
-
TOOLS_CALL: 'tools/call',
|
|
84
|
-
// Logging
|
|
85
|
-
LOGGING_MESSAGE: 'notifications/message',
|
|
86
|
-
// Progress
|
|
87
|
-
PROGRESS: 'notifications/progress',
|
|
88
|
-
};
|
|
89
|
-
// ============================================================================
|
|
90
|
-
// Error Codes (Standard MCP + Custom)
|
|
91
|
-
// ============================================================================
|
|
92
|
-
/** Standard JSON-RPC error codes */
|
|
93
|
-
export const JSONRPC_ERROR_CODES = {
|
|
94
|
-
PARSE_ERROR: -32700,
|
|
95
|
-
INVALID_REQUEST: -32600,
|
|
96
|
-
METHOD_NOT_FOUND: -32601,
|
|
97
|
-
INVALID_PARAMS: -32602,
|
|
98
|
-
INTERNAL_ERROR: -32603,
|
|
99
|
-
};
|
|
100
74
|
/** MemHub custom error codes */
|
|
101
75
|
export const MEMHUB_ERROR_CODES = {
|
|
102
76
|
NOT_FOUND: -32001,
|
|
@@ -104,9 +78,13 @@ export const MEMHUB_ERROR_CODES = {
|
|
|
104
78
|
VALIDATION_ERROR: -32003,
|
|
105
79
|
DUPLICATE_ERROR: -32004,
|
|
106
80
|
};
|
|
107
|
-
/** Combined error codes */
|
|
81
|
+
/** Combined error codes (includes JSON-RPC standard codes) */
|
|
108
82
|
export const ERROR_CODES = {
|
|
109
|
-
|
|
83
|
+
PARSE_ERROR: -32700,
|
|
84
|
+
INVALID_REQUEST: -32600,
|
|
85
|
+
METHOD_NOT_FOUND: -32601,
|
|
86
|
+
INVALID_PARAMS: -32602,
|
|
87
|
+
INTERNAL_ERROR: -32603,
|
|
110
88
|
...MEMHUB_ERROR_CODES,
|
|
111
89
|
};
|
|
112
90
|
//# sourceMappingURL=mcp.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mcp.js","sourceRoot":"","sources":["../../../src/contracts/mcp.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAIH,+EAA+E;AAC/E,uBAAuB;AACvB,+EAA+E;AAE/E,mCAAmC;AACnC,MAAM,CAAC,MAAM,oBAAoB,GAAG,YAAY,CAAC;AAEjD,yBAAyB;AACzB,MAAM,CAAC,MAAM,WAAW,GAAG;IACzB,IAAI,EAAE,QAAQ;IACd,OAAO,EAAE,OAAO;CACR,CAAC;
|
|
1
|
+
{"version":3,"file":"mcp.js","sourceRoot":"","sources":["../../../src/contracts/mcp.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAIH,+EAA+E;AAC/E,uBAAuB;AACvB,+EAA+E;AAE/E,mCAAmC;AACnC,MAAM,CAAC,MAAM,oBAAoB,GAAG,YAAY,CAAC;AAEjD,yBAAyB;AACzB,MAAM,CAAC,MAAM,WAAW,GAAG;IACzB,IAAI,EAAE,QAAQ;IACd,OAAO,EAAE,OAAO;CACR,CAAC;AAqBX,+EAA+E;AAC/E,gBAAgB;AAChB,+EAA+E;AAE/E,+BAA+B;A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/**
|
|
3
3
|
* MCP Server - Model Context Protocol server implementation
|
|
4
|
-
*
|
|
4
|
+
* Uses @modelcontextprotocol/sdk for protocol handling
|
|
5
5
|
*/
|
|
6
|
+
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
|
6
7
|
/**
|
|
7
|
-
*
|
|
8
|
+
* Create McpServer instance using SDK
|
|
8
9
|
*/
|
|
9
|
-
export declare
|
|
10
|
-
private readonly memoryService;
|
|
11
|
-
constructor();
|
|
12
|
-
/**
|
|
13
|
-
* Starts the MCP server and begins listening for requests on stdin
|
|
14
|
-
*/
|
|
15
|
-
start(): void;
|
|
16
|
-
/**
|
|
17
|
-
* Handles an incoming JSON-RPC message
|
|
18
|
-
*
|
|
19
|
-
* @param message - The JSON-RPC message string
|
|
20
|
-
*/
|
|
21
|
-
private handleMessage;
|
|
22
|
-
/**
|
|
23
|
-
* Handles a specific method call
|
|
24
|
-
*
|
|
25
|
-
* @param method - The method name
|
|
26
|
-
* @param params - The method parameters
|
|
27
|
-
* @returns The method result
|
|
28
|
-
*/
|
|
29
|
-
private handleMethod;
|
|
30
|
-
/**
|
|
31
|
-
* Handles the initialize method
|
|
32
|
-
*
|
|
33
|
-
* @param params - Initialize parameters
|
|
34
|
-
* @returns Initialize result
|
|
35
|
-
*/
|
|
36
|
-
private handleInitialize;
|
|
37
|
-
/**
|
|
38
|
-
* Handles tool calls
|
|
39
|
-
*
|
|
40
|
-
* @param request - Tool call request
|
|
41
|
-
* @returns Tool call result
|
|
42
|
-
*/
|
|
43
|
-
private handleToolCall;
|
|
44
|
-
/**
|
|
45
|
-
* Handles errors and sends appropriate error response
|
|
46
|
-
*
|
|
47
|
-
* @param id - Request ID
|
|
48
|
-
* @param error - The error that occurred
|
|
49
|
-
*/
|
|
50
|
-
private handleError;
|
|
51
|
-
/**
|
|
52
|
-
* Sends a JSON-RPC response
|
|
53
|
-
*
|
|
54
|
-
* @param id - Request ID
|
|
55
|
-
* @param result - Response result
|
|
56
|
-
*/
|
|
57
|
-
private sendResponse;
|
|
58
|
-
/**
|
|
59
|
-
* Sends a JSON-RPC error
|
|
60
|
-
*
|
|
61
|
-
* @param id - Request ID
|
|
62
|
-
* @param code - Error code
|
|
63
|
-
* @param message - Error message
|
|
64
|
-
* @param data - Additional error data
|
|
65
|
-
*/
|
|
66
|
-
private sendError;
|
|
67
|
-
/**
|
|
68
|
-
* Sends a message to stdout
|
|
69
|
-
*
|
|
70
|
-
* @param message - The message to send
|
|
71
|
-
*/
|
|
72
|
-
private sendMessage;
|
|
73
|
-
/**
|
|
74
|
-
* Logs a message
|
|
75
|
-
*
|
|
76
|
-
* @param level - Log level
|
|
77
|
-
* @param message - Log message
|
|
78
|
-
*/
|
|
79
|
-
private log;
|
|
80
|
-
}
|
|
10
|
+
export declare function createMcpServer(): Server;
|
|
81
11
|
//# sourceMappingURL=mcp-server.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mcp-server.d.ts","sourceRoot":"","sources":["../../../src/server/mcp-server.ts"],"names":[],"mappings":";AACA;;;GAGG;
|
|
1
|
+
{"version":3,"file":"mcp-server.d.ts","sourceRoot":"","sources":["../../../src/server/mcp-server.ts"],"names":[],"mappings":";AACA;;;GAGG;AAKH,OAAO,EAAE,MAAM,EAAE,MAAM,2CAA2C,CAAC;AA0BnE;;GAEG;AACH,wBAAgB,eAAe,IAAI,MAAM,CAyFxC"}
|
|
@@ -1,15 +1,18 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
/**
|
|
3
3
|
* MCP Server - Model Context Protocol server implementation
|
|
4
|
-
*
|
|
4
|
+
* Uses @modelcontextprotocol/sdk for protocol handling
|
|
5
5
|
*/
|
|
6
6
|
import { readFileSync } from 'fs';
|
|
7
7
|
import { join, dirname } from 'path';
|
|
8
8
|
import { fileURLToPath } from 'url';
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
9
|
+
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
|
10
|
+
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
11
|
+
import { CallToolRequestSchema, ListToolsRequestSchema, } from '@modelcontextprotocol/sdk/types.js';
|
|
11
12
|
import { MemoryService, ServiceError } from '../services/memory-service.js';
|
|
12
13
|
import { MemoryLoadInputSchema, MemoryUpdateInputV2Schema, } from '../contracts/schemas.js';
|
|
14
|
+
import { TOOL_DEFINITIONS, SERVER_INFO } from '../contracts/mcp.js';
|
|
15
|
+
import { ErrorCode } from '../contracts/types.js';
|
|
13
16
|
// Get package version
|
|
14
17
|
const __filename = fileURLToPath(import.meta.url);
|
|
15
18
|
const __dirname = dirname(__filename);
|
|
@@ -17,142 +20,39 @@ const __dirname = dirname(__filename);
|
|
|
17
20
|
const packageJsonPath = join(__dirname, '../../../package.json');
|
|
18
21
|
const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf-8'));
|
|
19
22
|
/**
|
|
20
|
-
*
|
|
23
|
+
* Create McpServer instance using SDK
|
|
21
24
|
*/
|
|
22
|
-
export
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
void this.handleMessage(line.trim());
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
});
|
|
46
|
-
process.stdin.on('end', () => {
|
|
47
|
-
this.log('info', 'Stdin closed, shutting down...');
|
|
48
|
-
process.exit(0);
|
|
49
|
-
});
|
|
50
|
-
process.on('SIGINT', () => {
|
|
51
|
-
this.log('info', 'Received SIGINT, shutting down...');
|
|
52
|
-
process.exit(0);
|
|
53
|
-
});
|
|
54
|
-
process.on('SIGTERM', () => {
|
|
55
|
-
this.log('info', 'Received SIGTERM, shutting down...');
|
|
56
|
-
process.exit(0);
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* Handles an incoming JSON-RPC message
|
|
61
|
-
*
|
|
62
|
-
* @param message - The JSON-RPC message string
|
|
63
|
-
*/
|
|
64
|
-
async handleMessage(message) {
|
|
65
|
-
let request = null;
|
|
66
|
-
try {
|
|
67
|
-
request = JSON.parse(message);
|
|
68
|
-
}
|
|
69
|
-
catch {
|
|
70
|
-
this.sendError(null, ERROR_CODES.PARSE_ERROR, 'Parse error: Invalid JSON');
|
|
71
|
-
return;
|
|
72
|
-
}
|
|
73
|
-
// Validate JSON-RPC request
|
|
74
|
-
if (request.jsonrpc !== '2.0' || !request.method) {
|
|
75
|
-
this.sendError(request.id ?? null, ERROR_CODES.INVALID_REQUEST, 'Invalid Request');
|
|
76
|
-
return;
|
|
77
|
-
}
|
|
78
|
-
try {
|
|
79
|
-
const result = await this.handleMethod(request.method, request.params);
|
|
80
|
-
// Send response (only for requests with id, not notifications)
|
|
81
|
-
if (request.id !== undefined) {
|
|
82
|
-
this.sendResponse(request.id, result);
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
catch (error) {
|
|
86
|
-
this.handleError(request.id ?? null, error);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
/**
|
|
90
|
-
* Handles a specific method call
|
|
91
|
-
*
|
|
92
|
-
* @param method - The method name
|
|
93
|
-
* @param params - The method parameters
|
|
94
|
-
* @returns The method result
|
|
95
|
-
*/
|
|
96
|
-
async handleMethod(method, params) {
|
|
97
|
-
switch (method) {
|
|
98
|
-
case MCP_METHODS.INITIALIZE:
|
|
99
|
-
return this.handleInitialize(params);
|
|
100
|
-
case MCP_METHODS.INITIALIZED:
|
|
101
|
-
// Notification, no response needed
|
|
102
|
-
this.log('info', 'Client initialized');
|
|
103
|
-
return null;
|
|
104
|
-
case MCP_METHODS.SHUTDOWN:
|
|
105
|
-
return null;
|
|
106
|
-
case MCP_METHODS.EXIT:
|
|
107
|
-
process.exit(0);
|
|
108
|
-
return null;
|
|
109
|
-
case MCP_METHODS.TOOLS_LIST:
|
|
110
|
-
return { tools: TOOL_DEFINITIONS };
|
|
111
|
-
case MCP_METHODS.TOOLS_CALL:
|
|
112
|
-
return this.handleToolCall(params);
|
|
113
|
-
default:
|
|
114
|
-
throw new ServiceError(`Method not found: ${method}`, ErrorCode.METHOD_NOT_FOUND);
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
/**
|
|
118
|
-
* Handles the initialize method
|
|
119
|
-
*
|
|
120
|
-
* @param params - Initialize parameters
|
|
121
|
-
* @returns Initialize result
|
|
122
|
-
*/
|
|
123
|
-
handleInitialize(params) {
|
|
124
|
-
this.log('info', `Client initializing: ${params.clientInfo.name} v${params.clientInfo.version}`);
|
|
125
|
-
return {
|
|
126
|
-
protocolVersion: MCP_PROTOCOL_VERSION,
|
|
127
|
-
capabilities: {
|
|
128
|
-
tools: { listChanged: false },
|
|
129
|
-
logging: {},
|
|
130
|
-
},
|
|
131
|
-
serverInfo: {
|
|
132
|
-
name: SERVER_INFO.name,
|
|
133
|
-
version: packageJson.version || SERVER_INFO.version,
|
|
134
|
-
},
|
|
135
|
-
};
|
|
136
|
-
}
|
|
137
|
-
/**
|
|
138
|
-
* Handles tool calls
|
|
139
|
-
*
|
|
140
|
-
* @param request - Tool call request
|
|
141
|
-
* @returns Tool call result
|
|
142
|
-
*/
|
|
143
|
-
async handleToolCall(request) {
|
|
144
|
-
const { name, arguments: args } = request;
|
|
25
|
+
export function createMcpServer() {
|
|
26
|
+
const storagePath = process.env.MEMHUB_STORAGE_PATH || './memories';
|
|
27
|
+
const memoryService = new MemoryService({ storagePath });
|
|
28
|
+
// Create server using SDK
|
|
29
|
+
const server = new Server({
|
|
30
|
+
name: SERVER_INFO.name,
|
|
31
|
+
version: packageJson.version || SERVER_INFO.version,
|
|
32
|
+
}, {
|
|
33
|
+
capabilities: {
|
|
34
|
+
tools: { listChanged: false },
|
|
35
|
+
logging: {},
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
// Handle tools/list request
|
|
39
|
+
server.setRequestHandler(ListToolsRequestSchema, async () => {
|
|
40
|
+
return { tools: TOOL_DEFINITIONS };
|
|
41
|
+
});
|
|
42
|
+
// Handle tools/call request
|
|
43
|
+
server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
44
|
+
const { name, arguments: args } = request.params;
|
|
145
45
|
try {
|
|
146
46
|
let result;
|
|
147
47
|
switch (name) {
|
|
148
48
|
case 'memory_load': {
|
|
149
49
|
const input = MemoryLoadInputSchema.parse(args ?? {});
|
|
150
|
-
result = await
|
|
50
|
+
result = await memoryService.memoryLoad(input);
|
|
151
51
|
break;
|
|
152
52
|
}
|
|
153
53
|
case 'memory_update': {
|
|
154
54
|
const input = MemoryUpdateInputV2Schema.parse(args ?? {});
|
|
155
|
-
result = await
|
|
55
|
+
result = await memoryService.memoryUpdate(input);
|
|
156
56
|
break;
|
|
157
57
|
}
|
|
158
58
|
default:
|
|
@@ -179,88 +79,34 @@ export class McpServer {
|
|
|
179
79
|
isError: true,
|
|
180
80
|
};
|
|
181
81
|
}
|
|
182
|
-
|
|
82
|
+
if (error instanceof Error && error.name === 'ZodError') {
|
|
83
|
+
return {
|
|
84
|
+
content: [
|
|
85
|
+
{
|
|
86
|
+
type: 'text',
|
|
87
|
+
text: JSON.stringify({ error: `Invalid parameters: ${error.message}` }, null, 2),
|
|
88
|
+
},
|
|
89
|
+
],
|
|
90
|
+
isError: true,
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
// Re-throw for SDK error handling
|
|
183
94
|
throw error;
|
|
184
95
|
}
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
* Handles errors and sends appropriate error response
|
|
188
|
-
*
|
|
189
|
-
* @param id - Request ID
|
|
190
|
-
* @param error - The error that occurred
|
|
191
|
-
*/
|
|
192
|
-
handleError(id, error) {
|
|
193
|
-
if (error instanceof ServiceError) {
|
|
194
|
-
this.sendError(id, error.code, error.message, error.data);
|
|
195
|
-
}
|
|
196
|
-
else if (error instanceof Error && error.name === 'ZodError') {
|
|
197
|
-
this.sendError(id, ERROR_CODES.INVALID_PARAMS, `Invalid parameters: ${error.message}`);
|
|
198
|
-
}
|
|
199
|
-
else {
|
|
200
|
-
this.sendError(id, ERROR_CODES.INTERNAL_ERROR, `Internal error: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
/**
|
|
204
|
-
* Sends a JSON-RPC response
|
|
205
|
-
*
|
|
206
|
-
* @param id - Request ID
|
|
207
|
-
* @param result - Response result
|
|
208
|
-
*/
|
|
209
|
-
sendResponse(id, result) {
|
|
210
|
-
const response = {
|
|
211
|
-
jsonrpc: '2.0',
|
|
212
|
-
id,
|
|
213
|
-
result,
|
|
214
|
-
};
|
|
215
|
-
this.sendMessage(response);
|
|
216
|
-
}
|
|
217
|
-
/**
|
|
218
|
-
* Sends a JSON-RPC error
|
|
219
|
-
*
|
|
220
|
-
* @param id - Request ID
|
|
221
|
-
* @param code - Error code
|
|
222
|
-
* @param message - Error message
|
|
223
|
-
* @param data - Additional error data
|
|
224
|
-
*/
|
|
225
|
-
sendError(id, code, message, data) {
|
|
226
|
-
const error = {
|
|
227
|
-
code,
|
|
228
|
-
message,
|
|
229
|
-
data,
|
|
230
|
-
};
|
|
231
|
-
const response = {
|
|
232
|
-
jsonrpc: '2.0',
|
|
233
|
-
id: id ?? null,
|
|
234
|
-
error,
|
|
235
|
-
};
|
|
236
|
-
this.sendMessage(response);
|
|
237
|
-
}
|
|
238
|
-
/**
|
|
239
|
-
* Sends a message to stdout
|
|
240
|
-
*
|
|
241
|
-
* @param message - The message to send
|
|
242
|
-
*/
|
|
243
|
-
sendMessage(message) {
|
|
244
|
-
const json = JSON.stringify(message);
|
|
245
|
-
process.stdout.write(json + '\n');
|
|
246
|
-
}
|
|
247
|
-
/**
|
|
248
|
-
* Logs a message
|
|
249
|
-
*
|
|
250
|
-
* @param level - Log level
|
|
251
|
-
* @param message - Log message
|
|
252
|
-
*/
|
|
253
|
-
log(level, message) {
|
|
254
|
-
const logLevel = process.env.MEMHUB_LOG_LEVEL || 'info';
|
|
255
|
-
const levels = { debug: 0, info: 1, warn: 2, error: 3 };
|
|
256
|
-
if (levels[level] >= levels[logLevel]) {
|
|
257
|
-
console.error(`[${level.toUpperCase()}] ${message}`);
|
|
258
|
-
}
|
|
259
|
-
}
|
|
96
|
+
});
|
|
97
|
+
return server;
|
|
260
98
|
}
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
99
|
+
/**
|
|
100
|
+
* Start the MCP server
|
|
101
|
+
*/
|
|
102
|
+
async function main() {
|
|
103
|
+
const server = createMcpServer();
|
|
104
|
+
const transport = new StdioServerTransport();
|
|
105
|
+
await server.connect(transport);
|
|
106
|
+
console.error('MemHub MCP Server running on stdio');
|
|
265
107
|
}
|
|
108
|
+
main().catch((error) => {
|
|
109
|
+
console.error('Fatal error:', error);
|
|
110
|
+
process.exit(1);
|
|
111
|
+
});
|
|
266
112
|
//# sourceMappingURL=mcp-server.js.map
|