@cslug/core 0.4.1 → 0.5.0
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/docs/README.md +3 -2
- package/docs/api.md +13 -0
- package/package.json +2 -2
- package/src/index.ts +9 -0
- package/src/mcp.ts +117 -0
package/docs/README.md
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
# @cslug/core
|
|
2
2
|
|
|
3
3
|
The application, the container with `@inject`, service providers, the ambient context, config and env, typed
|
|
4
|
-
errors, `Clock`, `Money`, events, the logger, i18n,
|
|
5
|
-
with in-memory drivers
|
|
4
|
+
errors, `Clock`, `Money`, events, the logger, i18n, the `Cache`/`Lock`/`RateLimiter`/`SessionStore` contracts
|
|
5
|
+
with in-memory drivers, and the server side of MCP (`handleMcpMessage`, `serveMcpStdio`) that `@cslug/ai` and
|
|
6
|
+
`cslug mcp:dev` share.
|
|
6
7
|
|
|
7
8
|
```ts
|
|
8
9
|
import { Application, inject, ServiceProvider } from '@cslug/core'
|
package/docs/api.md
CHANGED
|
@@ -198,3 +198,16 @@ Files: `<locale>.json`, and `<locale>/<namespace>.json` merged under the file na
|
|
|
198
198
|
|
|
199
199
|
Memory drivers: `MemoryCache` (values cloned, bigint and Date supported), `MemoryLock`, `MemoryRateLimiter`
|
|
200
200
|
(sliding window), `MemorySessionStore`. Each is one per application.
|
|
201
|
+
|
|
202
|
+
## MCP (server side)
|
|
203
|
+
|
|
204
|
+
The protocol, over any list of tools, shared by `@cslug/ai` (`@mcp()` tools at `POST /mcp`, `cslug mcp:serve`) and
|
|
205
|
+
`@cslug/cli` (`cslug mcp:dev`).
|
|
206
|
+
|
|
207
|
+
| | |
|
|
208
|
+
|---|---|
|
|
209
|
+
| `McpTool` | `{ name, description, inputSchema, call(args) }`: `call` validates its arguments; what it throws is an error result |
|
|
210
|
+
| `handleMcpMessage(message, tools, serverInfo?)` | One JSON-RPC message → its reply (undefined for a notification): `server/discover`, `initialize` (2025-11-25, 2025-06-18, 2025-03-26), `ping`, `tools/list`, `tools/call` (a plain-object result is also `structuredContent`), `-32022` for an unsupported per-request version, `-32601` for an unknown method |
|
|
211
|
+
| `serveMcpStdio(tools, serverInfo, io?)` | One message per line on stdin, replies on stdout, until stdin closes; a line that is not JSON gets `-32700` |
|
|
212
|
+
| `MCP_VERSIONS` | `2026-07-28` (stateless, `server/discover`) and the `initialize` versions |
|
|
213
|
+
| `McpMessage`, `McpReply`, `McpServerInfo` | The shapes |
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cslug/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Application, container and @inject, providers, config, events, logger, errors, context, Clock, Money, i18n, contracts.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -28,6 +28,6 @@
|
|
|
28
28
|
"bun": ">=1.3.14"
|
|
29
29
|
},
|
|
30
30
|
"peerDependencies": {
|
|
31
|
-
"@cslug/validation": "0.
|
|
31
|
+
"@cslug/validation": "0.5.0"
|
|
32
32
|
}
|
|
33
33
|
}
|
package/src/index.ts
CHANGED
|
@@ -103,6 +103,15 @@ export {
|
|
|
103
103
|
} from './i18n'
|
|
104
104
|
export { inject, type Lifetime } from './inject'
|
|
105
105
|
export { DEFAULT_REDACT, type LogFields, Logger, type LoggerOptions, type LogLevel } from './logger'
|
|
106
|
+
export {
|
|
107
|
+
handleMcpMessage,
|
|
108
|
+
MCP_VERSIONS,
|
|
109
|
+
type McpMessage,
|
|
110
|
+
type McpReply,
|
|
111
|
+
type McpServerInfo,
|
|
112
|
+
type McpTool,
|
|
113
|
+
serveMcpStdio,
|
|
114
|
+
} from './mcp'
|
|
106
115
|
export { Money, minorDigits } from './money'
|
|
107
116
|
export { type ProviderClass, type ProviderHooks, ServiceProvider } from './provider'
|
|
108
117
|
export { type Class, InjectionToken, type Resolved, type Token, token } from './token'
|
package/src/mcp.ts
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Model Context Protocol, server side, over any list of tools: `@cslug/ai` serves `@mcp()` tools with it
|
|
3
|
+
* (`POST /mcp`, `cslug mcp:serve`) and `@cslug/cli` the development tools (`cslug mcp:dev`).
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/** Protocol versions served: 2026-07-28 (stateless, `server/discover`) and the `initialize` ones before it. */
|
|
7
|
+
export const MCP_VERSIONS = ['2026-07-28', '2025-11-25', '2025-06-18', '2025-03-26']
|
|
8
|
+
const LEGACY = MCP_VERSIONS.slice(1)
|
|
9
|
+
const META = 'io.modelcontextprotocol/'
|
|
10
|
+
|
|
11
|
+
/** A tool as MCP sees it. `call` validates its arguments; what it throws becomes an error result the model reads. */
|
|
12
|
+
export type McpTool = {
|
|
13
|
+
name: string
|
|
14
|
+
description: string
|
|
15
|
+
inputSchema: Record<string, unknown>
|
|
16
|
+
call(args: Record<string, unknown>): unknown
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** A JSON-RPC message from the client. */
|
|
20
|
+
export type McpMessage = {
|
|
21
|
+
jsonrpc?: string
|
|
22
|
+
id?: string | number | null
|
|
23
|
+
method?: string
|
|
24
|
+
params?: Record<string, unknown>
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** A JSON-RPC response. */
|
|
28
|
+
export type McpReply = {
|
|
29
|
+
jsonrpc: string
|
|
30
|
+
id: string | number | null
|
|
31
|
+
result?: Record<string, unknown>
|
|
32
|
+
error?: { code: number; message: string; data?: unknown }
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export type McpServerInfo = { name: string; version: string }
|
|
36
|
+
|
|
37
|
+
const error = (id: McpMessage['id'], code: number, message: string, data?: unknown): McpReply => ({
|
|
38
|
+
jsonrpc: '2.0',
|
|
39
|
+
id: id ?? null,
|
|
40
|
+
error: { code, message, ...(data === undefined ? {} : { data }) },
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
/** One MCP message → its response (undefined for a notification). */
|
|
44
|
+
export async function handleMcpMessage(
|
|
45
|
+
message: McpMessage,
|
|
46
|
+
tools: readonly McpTool[],
|
|
47
|
+
serverInfo: McpServerInfo = { name: 'cslug', version: '0' },
|
|
48
|
+
): Promise<McpReply | undefined> {
|
|
49
|
+
const { id, method, params = {} } = message
|
|
50
|
+
if (id === undefined || id === null) return undefined // notifications (initialized, cancelled…)
|
|
51
|
+
const meta = (params._meta ?? {}) as Record<string, unknown>
|
|
52
|
+
const requested = meta[`${META}protocolVersion`]
|
|
53
|
+
if (typeof requested === 'string' && !MCP_VERSIONS.includes(requested))
|
|
54
|
+
return error(id, -32022, 'Unsupported protocol version', { supported: MCP_VERSIONS, requested })
|
|
55
|
+
const result = (value: object): McpReply => ({
|
|
56
|
+
jsonrpc: '2.0',
|
|
57
|
+
id,
|
|
58
|
+
result: { resultType: 'complete', ...value, _meta: { [`${META}serverInfo`]: serverInfo } },
|
|
59
|
+
})
|
|
60
|
+
switch (method) {
|
|
61
|
+
case 'server/discover':
|
|
62
|
+
return result({ supportedVersions: MCP_VERSIONS, capabilities: { tools: {} } })
|
|
63
|
+
case 'initialize': {
|
|
64
|
+
const asked = String(params.protocolVersion ?? '')
|
|
65
|
+
return result({
|
|
66
|
+
protocolVersion: LEGACY.includes(asked) ? asked : LEGACY[0],
|
|
67
|
+
capabilities: { tools: {} },
|
|
68
|
+
serverInfo,
|
|
69
|
+
})
|
|
70
|
+
}
|
|
71
|
+
case 'ping':
|
|
72
|
+
return result({})
|
|
73
|
+
case 'tools/list':
|
|
74
|
+
return result({ tools: tools.map(({ name, description, inputSchema }) => ({ name, description, inputSchema })) })
|
|
75
|
+
case 'tools/call': {
|
|
76
|
+
const tool = tools.find((t) => t.name === params.name)
|
|
77
|
+
if (!tool) return error(id, -32602, `Unknown tool: ${String(params.name)}`)
|
|
78
|
+
try {
|
|
79
|
+
const value = await tool.call((params.arguments ?? {}) as Record<string, unknown>)
|
|
80
|
+
const plain = value !== null && typeof value === 'object' && !Array.isArray(value)
|
|
81
|
+
return result({
|
|
82
|
+
content: [{ type: 'text', text: typeof value === 'string' ? value : JSON.stringify(value ?? null) }],
|
|
83
|
+
isError: false,
|
|
84
|
+
...(plain ? { structuredContent: value } : {}),
|
|
85
|
+
})
|
|
86
|
+
} catch (e) {
|
|
87
|
+
return result({ content: [{ type: 'text', text: e instanceof Error ? e.message : String(e) }], isError: true })
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
default:
|
|
91
|
+
return error(id, -32601, `Method not found: ${String(method)}`)
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Serve MCP over stdio: one JSON-RPC message per line on stdin, replies on stdout (nothing else may write there:
|
|
97
|
+
* route logs to stderr first). Returns when stdin closes.
|
|
98
|
+
*/
|
|
99
|
+
export async function serveMcpStdio(
|
|
100
|
+
tools: () => readonly McpTool[] | Promise<readonly McpTool[]>,
|
|
101
|
+
serverInfo: McpServerInfo,
|
|
102
|
+
io: { lines: AsyncIterable<string>; write(line: string): void } = {
|
|
103
|
+
lines: console,
|
|
104
|
+
write: (line) => process.stdout.write(`${line}\n`),
|
|
105
|
+
},
|
|
106
|
+
): Promise<void> {
|
|
107
|
+
for await (const line of io.lines) {
|
|
108
|
+
if (!line.trim()) continue
|
|
109
|
+
let reply: McpReply | undefined
|
|
110
|
+
try {
|
|
111
|
+
reply = await handleMcpMessage(JSON.parse(line) as McpMessage, await tools(), serverInfo)
|
|
112
|
+
} catch {
|
|
113
|
+
reply = error(null, -32700, 'Parse error')
|
|
114
|
+
}
|
|
115
|
+
if (reply) io.write(JSON.stringify(reply))
|
|
116
|
+
}
|
|
117
|
+
}
|