@mcpcn/mcp-notification 1.0.16 → 1.0.18
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/dist/index.js +50 -11
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -12,22 +12,57 @@ function resolveChatSessionId(request) {
|
|
|
12
12
|
}
|
|
13
13
|
return undefined;
|
|
14
14
|
};
|
|
15
|
-
|
|
16
|
-
pick(request?.params?.meta, ['chatSessionId', 'chatSessionID'])
|
|
17
|
-
pick(request?.meta, ['chatSessionId', 'chatSessionID'])
|
|
18
|
-
pick(request?._meta, ['chatSessionId', 'chatSessionID'])
|
|
15
|
+
const direct = pick(request?.params?._meta, ['chatSessionId', 'chatSessionID']) ||
|
|
16
|
+
pick(request?.params?.meta, ['chatSessionId', 'chatSessionID']) ||
|
|
17
|
+
pick(request?.meta, ['chatSessionId', 'chatSessionID']) ||
|
|
18
|
+
pick(request?._meta, ['chatSessionId', 'chatSessionID']);
|
|
19
|
+
if (direct)
|
|
20
|
+
return direct;
|
|
21
|
+
const inArgs = pick(request?.params?.arguments?._meta, ['chatSessionId', 'chatSessionID']) ||
|
|
22
|
+
pick(request?.params?.arguments?.meta, ['chatSessionId', 'chatSessionID']) ||
|
|
23
|
+
pick(request?.params?.arguments, ['chatSessionId', 'chatSessionID']);
|
|
24
|
+
if (inArgs)
|
|
25
|
+
return inArgs;
|
|
26
|
+
const scan = (obj, keys, maxDepth = 3) => {
|
|
27
|
+
const queue = [obj];
|
|
28
|
+
const seen = new Set();
|
|
29
|
+
let depth = 0;
|
|
30
|
+
while (queue.length && depth <= maxDepth) {
|
|
31
|
+
const cur = queue.shift();
|
|
32
|
+
if (!cur || typeof cur !== 'object' || seen.has(cur)) {
|
|
33
|
+
depth++;
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
seen.add(cur);
|
|
37
|
+
for (const k of keys) {
|
|
38
|
+
const v = cur[k];
|
|
39
|
+
if (typeof v === 'string' && v)
|
|
40
|
+
return v;
|
|
41
|
+
}
|
|
42
|
+
for (const v of Object.values(cur)) {
|
|
43
|
+
if (v && typeof v === 'object')
|
|
44
|
+
queue.push(v);
|
|
45
|
+
}
|
|
46
|
+
depth++;
|
|
47
|
+
}
|
|
48
|
+
return undefined;
|
|
49
|
+
};
|
|
50
|
+
return scan(request, ['chatSessionId', 'chatSessionID']);
|
|
19
51
|
}
|
|
20
52
|
async function postJson(path, body, chatSessionId) {
|
|
21
53
|
const headers = { 'Content-Type': 'application/json', Accept: 'application/json' };
|
|
22
|
-
if (chatSessionId)
|
|
54
|
+
if (chatSessionId) {
|
|
23
55
|
headers['chatSessionId'] = chatSessionId;
|
|
56
|
+
headers['x-chat-session-id'] = chatSessionId;
|
|
57
|
+
}
|
|
58
|
+
const payload = chatSessionId ? { ...body, chatSessionId } : body;
|
|
24
59
|
let lastError;
|
|
25
60
|
for (let attempt = 0; attempt < 3; attempt++) {
|
|
26
61
|
try {
|
|
27
62
|
const resp = await fetch(`${API_BASE}${path}`, {
|
|
28
63
|
method: 'POST',
|
|
29
64
|
headers,
|
|
30
|
-
body: JSON.stringify(
|
|
65
|
+
body: JSON.stringify(payload),
|
|
31
66
|
});
|
|
32
67
|
if (!resp.ok) {
|
|
33
68
|
const text = await resp.text().catch(() => '');
|
|
@@ -52,12 +87,16 @@ async function postJson(path, body, chatSessionId) {
|
|
|
52
87
|
}
|
|
53
88
|
async function getJson(path, chatSessionId) {
|
|
54
89
|
const headers = { Accept: 'application/json' };
|
|
55
|
-
if (chatSessionId)
|
|
90
|
+
if (chatSessionId) {
|
|
56
91
|
headers['chatSessionId'] = chatSessionId;
|
|
92
|
+
}
|
|
57
93
|
let lastError;
|
|
58
94
|
for (let attempt = 0; attempt < 3; attempt++) {
|
|
59
95
|
try {
|
|
60
|
-
const
|
|
96
|
+
const url = new URL(`${API_BASE}${path}`);
|
|
97
|
+
if (chatSessionId)
|
|
98
|
+
url.searchParams.set('chatSessionId', chatSessionId);
|
|
99
|
+
const resp = await fetch(url.toString(), { headers });
|
|
61
100
|
if (!resp.ok) {
|
|
62
101
|
const text = await resp.text().catch(() => '');
|
|
63
102
|
const msg = `HTTP 错误: ${resp.status} ${resp.statusText}${text ? ` | 响应体: ${text.slice(0, 500)}` : ''}`;
|
|
@@ -132,7 +171,7 @@ class ReminderServer {
|
|
|
132
171
|
},
|
|
133
172
|
],
|
|
134
173
|
}));
|
|
135
|
-
this.server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
174
|
+
this.server.setRequestHandler((CallToolRequestSchema.passthrough ? CallToolRequestSchema.passthrough() : CallToolRequestSchema), async (request) => {
|
|
136
175
|
try {
|
|
137
176
|
if (!request.params.arguments || typeof request.params.arguments !== 'object') {
|
|
138
177
|
throw new McpError(ErrorCode.InvalidParams, '无效的参数');
|
|
@@ -141,7 +180,7 @@ class ReminderServer {
|
|
|
141
180
|
const args = request.params.arguments;
|
|
142
181
|
const chatSessionId = resolveChatSessionId(request);
|
|
143
182
|
if (!chatSessionId) {
|
|
144
|
-
console.error('
|
|
183
|
+
console.error('工具未在请求中检测到 chatSessionId');
|
|
145
184
|
const baseMsg = '解析设备失败: chatSessionId不能为空,工具暂无法使用';
|
|
146
185
|
const errText = name === 'set_reminder'
|
|
147
186
|
? `设置失败:${baseMsg}`
|
|
@@ -153,7 +192,7 @@ class ReminderServer {
|
|
|
153
192
|
return { content: [{ type: 'text', text: errText }], isError: true };
|
|
154
193
|
}
|
|
155
194
|
else {
|
|
156
|
-
console.error(
|
|
195
|
+
console.error(`工具接收到 chatSessionId: ${chatSessionId}`);
|
|
157
196
|
}
|
|
158
197
|
if (name === 'set_reminder') {
|
|
159
198
|
const params = {
|