tiny-stdio-mcp-server 0.1.13 → 0.1.15
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 +33 -13
- package/dist/jsonrpc.js +2 -1
- package/dist/server.js +43 -18
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -14,7 +14,7 @@ npm install tiny-stdio-mcp-server
|
|
|
14
14
|
import { createServer, defineSchema } from "tiny-stdio-mcp-server";
|
|
15
15
|
|
|
16
16
|
const schema = defineSchema({
|
|
17
|
-
text: { type: "string", description: "Text to reverse" }
|
|
17
|
+
text: { type: "string", description: "Text to reverse" }
|
|
18
18
|
});
|
|
19
19
|
|
|
20
20
|
createServer({ name: "my-server", version: "1.0.0" })
|
|
@@ -49,7 +49,7 @@ Register a tool. The handler receives typed args matching the schema and returns
|
|
|
49
49
|
```ts
|
|
50
50
|
const schema = defineSchema({
|
|
51
51
|
query: { type: "string", description: "Search query" },
|
|
52
|
-
limit: { type: "number", description: "Max results", optional: true }
|
|
52
|
+
limit: { type: "number", description: "Max results", optional: true }
|
|
53
53
|
});
|
|
54
54
|
|
|
55
55
|
server.tool("search", "Search for things", schema, async ({ query, limit }) => {
|
|
@@ -62,7 +62,7 @@ For structured-data tools, pass a root-object output schema. The server advertis
|
|
|
62
62
|
|
|
63
63
|
```ts
|
|
64
64
|
const input = defineSchema({
|
|
65
|
-
query: { type: "string" }
|
|
65
|
+
query: { type: "string" }
|
|
66
66
|
});
|
|
67
67
|
const output = defineSchema({
|
|
68
68
|
items: {
|
|
@@ -71,16 +71,22 @@ const output = defineSchema({
|
|
|
71
71
|
type: "object",
|
|
72
72
|
properties: {
|
|
73
73
|
title: { type: "string" },
|
|
74
|
-
score: { type: "number" }
|
|
74
|
+
score: { type: "number" }
|
|
75
75
|
},
|
|
76
|
-
required: ["title", "score"]
|
|
77
|
-
}
|
|
78
|
-
}
|
|
76
|
+
required: ["title", "score"]
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
79
|
});
|
|
80
80
|
|
|
81
|
-
server.tool(
|
|
82
|
-
|
|
83
|
-
|
|
81
|
+
server.tool(
|
|
82
|
+
"search",
|
|
83
|
+
"Search",
|
|
84
|
+
input,
|
|
85
|
+
async ({ query }) => ({
|
|
86
|
+
items: [{ title: query, score: 1 }]
|
|
87
|
+
}),
|
|
88
|
+
output
|
|
89
|
+
);
|
|
84
90
|
```
|
|
85
91
|
|
|
86
92
|
Output schemas must have `type: "object"` at the root. Tools whose natural result is prose, images, audio, files, or other content blocks should omit `outputSchema` and keep returning content.
|
|
@@ -113,6 +119,12 @@ await server.connectSDK(sdkTransport);
|
|
|
113
119
|
|
|
114
120
|
Dynamically add/remove tools at runtime and notify connected clients.
|
|
115
121
|
|
|
122
|
+
## Configuration Options
|
|
123
|
+
|
|
124
|
+
- `createServer({ name, version })`: server identity sent during MCP initialization.
|
|
125
|
+
- `.tool(name, description, schema, handler, outputSchema?)`: tool metadata, input schema, handler, and optional structured output schema.
|
|
126
|
+
- `.connect({ readable, writable })`: custom stream transport for tests or embedded hosts.
|
|
127
|
+
|
|
116
128
|
## `defineSchema(definition)`
|
|
117
129
|
|
|
118
130
|
Type-safe schema builder. Returns a JSON Schema object with inferred TypeScript types.
|
|
@@ -120,11 +132,15 @@ Type-safe schema builder. Returns a JSON Schema object with inferred TypeScript
|
|
|
120
132
|
```ts
|
|
121
133
|
const schema = defineSchema({
|
|
122
134
|
name: { type: "string", description: "User name" },
|
|
123
|
-
age: { type: "number", description: "User age", optional: true }
|
|
135
|
+
age: { type: "number", description: "User age", optional: true }
|
|
124
136
|
});
|
|
125
137
|
// Handler receives: { name: string; age?: number }
|
|
126
138
|
```
|
|
127
139
|
|
|
140
|
+
## Environment Variables
|
|
141
|
+
|
|
142
|
+
This package does not read public environment variables.
|
|
143
|
+
|
|
128
144
|
Supported types: `string`, `number`, `boolean`, `object`, `array`.
|
|
129
145
|
|
|
130
146
|
## Content helpers
|
|
@@ -189,8 +205,12 @@ Use `createTestPair` with the official MCP SDK for in-memory testing:
|
|
|
189
205
|
```ts
|
|
190
206
|
import { createTestPair } from "tiny-stdio-mcp-server/testing";
|
|
191
207
|
|
|
192
|
-
const server = createServer({ name: "test", version: "1.0.0" })
|
|
193
|
-
|
|
208
|
+
const server = createServer({ name: "test", version: "1.0.0" }).tool(
|
|
209
|
+
"ping",
|
|
210
|
+
"Ping",
|
|
211
|
+
defineSchema({}),
|
|
212
|
+
() => "pong"
|
|
213
|
+
);
|
|
194
214
|
|
|
195
215
|
const { client, cleanup } = await createTestPair(server);
|
|
196
216
|
|
package/dist/jsonrpc.js
CHANGED
package/dist/server.js
CHANGED
|
@@ -292,7 +292,21 @@ export function createServer(options) {
|
|
|
292
292
|
}) + "\n");
|
|
293
293
|
return;
|
|
294
294
|
}
|
|
295
|
-
|
|
295
|
+
let handled;
|
|
296
|
+
try {
|
|
297
|
+
handled = await messageHandler(request.method, request.params);
|
|
298
|
+
}
|
|
299
|
+
catch {
|
|
300
|
+
if (!isNotification) {
|
|
301
|
+
const requestWithId = request;
|
|
302
|
+
write(formatErrorResponse(requestWithId.id, {
|
|
303
|
+
code: JSON_RPC_ERROR_CODES.INTERNAL_ERROR,
|
|
304
|
+
message: "Internal error",
|
|
305
|
+
}) + "\n");
|
|
306
|
+
}
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
const { result, error } = handled;
|
|
296
310
|
if (isNotification) {
|
|
297
311
|
return;
|
|
298
312
|
}
|
|
@@ -412,20 +426,17 @@ export function createServer(options) {
|
|
|
412
426
|
},
|
|
413
427
|
async connect(transport) {
|
|
414
428
|
return new Promise((resolve) => {
|
|
415
|
-
const lifecycle = { initialized: false, initializeAccepted: false, notificationReady: false, resourceSubscriptions: new Set() };
|
|
416
|
-
const messageHandler = (method, params) => handleMessageWithLifecycle(method, lifecycle, params);
|
|
417
|
-
messageLifecycles.add(lifecycle);
|
|
418
429
|
const listener = (notification) => {
|
|
419
430
|
transport.writable.write(`${JSON.stringify(notification)}\n`);
|
|
420
431
|
};
|
|
421
|
-
|
|
432
|
+
const session = server.createMessageSession(listener);
|
|
422
433
|
const rl = readline.createInterface({
|
|
423
434
|
input: transport.readable,
|
|
424
435
|
crlfDelay: Infinity,
|
|
425
436
|
});
|
|
426
437
|
const pendingMessages = new Set();
|
|
427
438
|
rl.on("line", (line) => {
|
|
428
|
-
const message = processLine(line, (data) => transport.writable.write(data),
|
|
439
|
+
const message = processLine(line, (data) => transport.writable.write(data), session.handleMessage);
|
|
429
440
|
pendingMessages.add(message);
|
|
430
441
|
void message.finally(() => {
|
|
431
442
|
pendingMessages.delete(message);
|
|
@@ -433,19 +444,15 @@ export function createServer(options) {
|
|
|
433
444
|
});
|
|
434
445
|
rl.on("close", async () => {
|
|
435
446
|
await Promise.all([...pendingMessages]);
|
|
436
|
-
|
|
437
|
-
messageLifecycles.delete(lifecycle);
|
|
447
|
+
session.close();
|
|
438
448
|
resolve();
|
|
439
449
|
});
|
|
440
450
|
});
|
|
441
451
|
},
|
|
442
452
|
async connectSDK(transport) {
|
|
443
453
|
return new Promise((resolve, reject) => {
|
|
444
|
-
const lifecycle = { initialized: false, initializeAccepted: false, notificationReady: false, resourceSubscriptions: new Set() };
|
|
445
|
-
const messageHandler = (method, params) => handleMessageWithLifecycle(method, lifecycle, params);
|
|
446
|
-
messageLifecycles.add(lifecycle);
|
|
447
454
|
const listener = (notification) => transport.send(notification);
|
|
448
|
-
|
|
455
|
+
const session = server.createMessageSession(listener);
|
|
449
456
|
transport.onmessage = async (message) => {
|
|
450
457
|
// Ignore responses (we only handle requests/notifications)
|
|
451
458
|
if (!("method" in message)) {
|
|
@@ -456,7 +463,12 @@ export function createServer(options) {
|
|
|
456
463
|
if (message.method === "initialize") {
|
|
457
464
|
return;
|
|
458
465
|
}
|
|
459
|
-
|
|
466
|
+
try {
|
|
467
|
+
await session.handleMessage(message.method, message.params);
|
|
468
|
+
}
|
|
469
|
+
catch {
|
|
470
|
+
return;
|
|
471
|
+
}
|
|
460
472
|
return;
|
|
461
473
|
}
|
|
462
474
|
if (message.method === "notifications/initialized") {
|
|
@@ -471,7 +483,22 @@ export function createServer(options) {
|
|
|
471
483
|
return;
|
|
472
484
|
}
|
|
473
485
|
const request = message;
|
|
474
|
-
|
|
486
|
+
let handled;
|
|
487
|
+
try {
|
|
488
|
+
handled = await session.handleMessage(request.method, request.params);
|
|
489
|
+
}
|
|
490
|
+
catch {
|
|
491
|
+
await transport.send({
|
|
492
|
+
jsonrpc: "2.0",
|
|
493
|
+
id: request.id,
|
|
494
|
+
error: {
|
|
495
|
+
code: JSON_RPC_ERROR_CODES.INTERNAL_ERROR,
|
|
496
|
+
message: "Internal error",
|
|
497
|
+
},
|
|
498
|
+
});
|
|
499
|
+
return;
|
|
500
|
+
}
|
|
501
|
+
const { result, error } = handled;
|
|
475
502
|
if (error) {
|
|
476
503
|
const response = {
|
|
477
504
|
jsonrpc: "2.0",
|
|
@@ -490,13 +517,11 @@ export function createServer(options) {
|
|
|
490
517
|
}
|
|
491
518
|
};
|
|
492
519
|
transport.onclose = () => {
|
|
493
|
-
|
|
494
|
-
messageLifecycles.delete(lifecycle);
|
|
520
|
+
session.close();
|
|
495
521
|
resolve();
|
|
496
522
|
};
|
|
497
523
|
void transport.start().catch((error) => {
|
|
498
|
-
|
|
499
|
-
messageLifecycles.delete(lifecycle);
|
|
524
|
+
session.close();
|
|
500
525
|
reject(error);
|
|
501
526
|
});
|
|
502
527
|
});
|