@casys/mcp-server 0.26.1 → 0.27.1
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/CHANGELOG.md +15 -0
- package/LICENSE +21 -0
- package/README.md +10 -627
- package/esm/mod.d.ts +1 -0
- package/esm/mod.js +1 -0
- package/package.json +28 -25
- package/mod.ts +0 -302
- package/src/auth/as-metadata-proxy.ts +0 -288
- package/src/auth/config.ts +0 -368
- package/src/auth/jwt-provider.ts +0 -327
- package/src/auth/middleware.ts +0 -257
- package/src/auth/mod.ts +0 -89
- package/src/auth/multi-tenant-middleware.ts +0 -236
- package/src/auth/presets.ts +0 -351
- package/src/auth/provider.ts +0 -49
- package/src/auth/scope-middleware.ts +0 -59
- package/src/auth/static-token-provider.ts +0 -197
- package/src/auth/types.ts +0 -207
- package/src/client-auth/callback-server.ts +0 -166
- package/src/client-auth/client-id-metadata.ts +0 -260
- package/src/client-auth/connect.ts +0 -72
- package/src/client-auth/mod.ts +0 -44
- package/src/client-auth/provider.ts +0 -300
- package/src/client-auth/token-store/file-store.ts +0 -73
- package/src/client-auth/token-store/memory-store.ts +0 -32
- package/src/client-auth/types.ts +0 -95
- package/src/concurrency/rate-limiter.ts +0 -190
- package/src/concurrency/request-queue.ts +0 -140
- package/src/http/body.ts +0 -72
- package/src/http/request-guards.ts +0 -137
- package/src/http/wire.ts +0 -41
- package/src/inspector/launcher.ts +0 -131
- package/src/mcp-app.ts +0 -4595
- package/src/middleware/backpressure.ts +0 -36
- package/src/middleware/mod.ts +0 -21
- package/src/middleware/rate-limit.ts +0 -45
- package/src/middleware/runner.ts +0 -63
- package/src/middleware/types.ts +0 -104
- package/src/middleware/validation.ts +0 -28
- package/src/mrtr/capability-check.ts +0 -303
- package/src/mrtr/mod.ts +0 -54
- package/src/mrtr/replay-store.ts +0 -103
- package/src/mrtr/request-state.ts +0 -435
- package/src/mrtr/retry-guard.ts +0 -110
- package/src/mrtr/types.ts +0 -107
- package/src/observability/metrics.ts +0 -336
- package/src/observability/mod.ts +0 -21
- package/src/observability/otel.ts +0 -330
- package/src/runtime/runtime.deno.ts +0 -149
- package/src/runtime/runtime.node.ts +0 -324
- package/src/runtime/runtime.ts +0 -50
- package/src/runtime/types.ts +0 -130
- package/src/security/channel-hmac.ts +0 -140
- package/src/security/csp.ts +0 -89
- package/src/security/message-signer.ts +0 -223
- package/src/subscriptions/subscription-registry.ts +0 -558
- package/src/tasks/mod.ts +0 -33
- package/src/tasks/task-store.ts +0 -1014
- package/src/transport/request-headers.ts +0 -661
- package/src/types.ts +0 -908
- package/src/ui/viewer-utils.ts +0 -117
- package/src/validation/schema-validator.ts +0 -307
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to `@casys/mcp-server` will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and the project adheres
|
|
6
|
+
to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [Unreleased]
|
|
9
|
+
|
|
10
|
+
## [0.27.1] - 2026-09-18
|
|
11
|
+
|
|
12
|
+
### Changed
|
|
13
|
+
|
|
14
|
+
- **Deprecated alias:** the framework moved to `@casys/mcp-platform@^0.28.0`. This package now
|
|
15
|
+
re-exports it unchanged. Framework history continues in the `@casys/mcp-platform` changelog.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Casys AI
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,638 +1,21 @@
|
|
|
1
|
-
# @casys/mcp-server
|
|
1
|
+
# @casys/mcp-server (deprecated alias)
|
|
2
2
|
|
|
3
|
-
[](https://www.npmjs.com/package/@casys/mcp-server)
|
|
4
|
-
[](https://jsr.io/@casys/mcp-server)
|
|
5
3
|
[](LICENSE)
|
|
6
4
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
everything you need to ship reliable MCP servers. Built on the official
|
|
12
|
-
[`@modelcontextprotocol/server`](https://www.npmjs.com/package/@modelcontextprotocol/server)
|
|
13
|
-
and
|
|
14
|
-
[`@modelcontextprotocol/sdk`](https://github.com/modelcontextprotocol/typescript-sdk)
|
|
15
|
-
packages.
|
|
16
|
-
|
|
17
|
-
```
|
|
18
|
-
rate-limit → auth → custom middleware → scope-check → validation → backpressure → handler
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
---
|
|
22
|
-
|
|
23
|
-
## Why @casys/mcp-server?
|
|
24
|
-
|
|
25
|
-
The official SDK gives you the protocol. This framework gives you the production
|
|
26
|
-
stack.
|
|
27
|
-
|
|
28
|
-
| | Official SDK | @casys/mcp-server |
|
|
29
|
-
| --------------------------- | :----------: | :----------------------------: |
|
|
30
|
-
| MCP protocol compliance | Yes | Yes |
|
|
31
|
-
| Concurrency control | -- | 3 backpressure strategies |
|
|
32
|
-
| Middleware pipeline | -- | Composable onion model |
|
|
33
|
-
| OAuth2 / JWT auth | -- | Built-in + 4 OIDC presets |
|
|
34
|
-
| Rate limiting | -- | Sliding window, per-client |
|
|
35
|
-
| Schema validation | -- | JSON Schema (ajv) |
|
|
36
|
-
| Streamable HTTP (stateless) | Manual | `startHttp()` / handler |
|
|
37
|
-
| OpenTelemetry tracing | -- | Automatic spans per tool call |
|
|
38
|
-
| Prometheus metrics | -- | `/metrics` endpoint |
|
|
39
|
-
| MCP Apps (UI resources) | Manual | `registerResource()` + `ui://` |
|
|
40
|
-
|
|
41
|
-
---
|
|
42
|
-
|
|
43
|
-
## Install
|
|
5
|
+
> **Deprecated:** the framework moved to
|
|
6
|
+
> [`@casys/mcp-platform`](https://github.com/Casys-AI/mcp-platform/blob/main/packages/platform/README.md).
|
|
7
|
+
> This package re-exports it unchanged so existing imports keep working. New code should install
|
|
8
|
+
> `@casys/mcp-platform` directly.
|
|
44
9
|
|
|
45
10
|
```bash
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
# Node (secondary — npm, via build-node compilation)
|
|
50
|
-
npm install @casys/mcp-server
|
|
11
|
+
npm install @casys/mcp-server # works, but deprecated
|
|
12
|
+
npm install @casys/mcp-platform # canonical
|
|
51
13
|
```
|
|
52
14
|
|
|
53
|
-
## Runtime targets
|
|
54
|
-
|
|
55
|
-
`@casys/mcp-server` is **Deno-first**. The canonical deployment path is Deno 2.x
|
|
56
|
-
running on [Deno Deploy](https://deno.com/deploy) or self-hosted Deno, with a
|
|
57
|
-
Node 20+ distribution as a secondary target via `scripts/build-node.sh` (which
|
|
58
|
-
swaps the HTTP runtime adapter and remaps `@std/*` imports to their npm
|
|
59
|
-
equivalents).
|
|
60
|
-
|
|
61
|
-
| Runtime | Status |
|
|
62
|
-
| --------------------------------------------- | :--------------: |
|
|
63
|
-
| **Deno 2.x** (Deno Deploy, self-hosted) | ✅ Primary |
|
|
64
|
-
| **Node.js 20+** (Express, Hono-on-Node, bare) | ✅ Secondary |
|
|
65
|
-
| **Cloudflare Workers / workerd** | ❌ Not supported |
|
|
66
|
-
| **Browser / WebContainer** | ❌ Not supported |
|
|
67
|
-
|
|
68
|
-
If you need to target Cloudflare Workers or the browser, use
|
|
69
|
-
[`@modelcontextprotocol/server`](https://www.npmjs.com/package/@modelcontextprotocol/server)
|
|
70
|
-
directly with its workerd / browser shims — that package focuses on the protocol
|
|
71
|
-
and runtime portability, while `@casys/mcp-server` focuses on the production
|
|
72
|
-
stack (auth, middleware, observability, multi-tenant, MCP Apps helpers) for Deno
|
|
73
|
-
deployments.
|
|
74
|
-
|
|
75
|
-
---
|
|
76
|
-
|
|
77
|
-
## Quick Start
|
|
78
|
-
|
|
79
|
-
### STDIO Server (5 lines)
|
|
80
|
-
|
|
81
15
|
```typescript
|
|
16
|
+
// Old imports keep working:
|
|
82
17
|
import { McpApp } from "@casys/mcp-server";
|
|
83
18
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
server.registerTool(
|
|
87
|
-
{
|
|
88
|
-
name: "greet",
|
|
89
|
-
description: "Greet a user",
|
|
90
|
-
inputSchema: {
|
|
91
|
-
type: "object",
|
|
92
|
-
properties: { name: { type: "string" } },
|
|
93
|
-
required: ["name"],
|
|
94
|
-
},
|
|
95
|
-
},
|
|
96
|
-
({ name }) => `Hello, ${name}!`,
|
|
97
|
-
);
|
|
98
|
-
|
|
99
|
-
await server.start();
|
|
100
|
-
```
|
|
101
|
-
|
|
102
|
-
### HTTP Server with Auth
|
|
103
|
-
|
|
104
|
-
```typescript
|
|
105
|
-
import { createGoogleAuthProvider, McpApp } from "@casys/mcp-server";
|
|
106
|
-
|
|
107
|
-
const server = new McpApp({
|
|
108
|
-
name: "my-api",
|
|
109
|
-
version: "1.0.0",
|
|
110
|
-
maxConcurrent: 10,
|
|
111
|
-
backpressureStrategy: "queue",
|
|
112
|
-
validateSchema: true,
|
|
113
|
-
rateLimit: { maxRequests: 100, windowMs: 60_000 },
|
|
114
|
-
auth: {
|
|
115
|
-
provider: createGoogleAuthProvider({
|
|
116
|
-
audience: "https://my-mcp.example.com",
|
|
117
|
-
resource: "https://my-mcp.example.com",
|
|
118
|
-
}),
|
|
119
|
-
},
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
server.registerTool(
|
|
123
|
-
{
|
|
124
|
-
name: "query",
|
|
125
|
-
description: "Query the database",
|
|
126
|
-
inputSchema: {
|
|
127
|
-
type: "object",
|
|
128
|
-
properties: { sql: { type: "string" } },
|
|
129
|
-
},
|
|
130
|
-
requiredScopes: ["db:read"],
|
|
131
|
-
},
|
|
132
|
-
async ({ sql }) => ({ rows: [] }),
|
|
133
|
-
);
|
|
134
|
-
|
|
135
|
-
await server.startHttp({ port: 3000 });
|
|
136
|
-
// GET /health → { status: "ok" }
|
|
137
|
-
// GET /metrics → Prometheus text format
|
|
138
|
-
// POST /mcp → JSON-RPC (tools/call, tools/list, ...)
|
|
139
|
-
// GET /mcp → 405 Method Not Allowed (stateless transport)
|
|
140
|
-
```
|
|
141
|
-
|
|
142
|
-
**Secure-by-default HTTP options:**
|
|
143
|
-
|
|
144
|
-
```typescript
|
|
145
|
-
await server.startHttp({
|
|
146
|
-
port: 3000,
|
|
147
|
-
requireAuth: true, // fail fast if auth isn't configured
|
|
148
|
-
corsOrigins: ["https://app.example.com"],
|
|
149
|
-
maxBodyBytes: 1_000_000, // 1 MB
|
|
150
|
-
ipRateLimit: { maxRequests: 120, windowMs: 60_000 },
|
|
151
|
-
});
|
|
152
|
-
```
|
|
153
|
-
|
|
154
|
-
**Notes:**
|
|
155
|
-
|
|
156
|
-
- `requireAuth: true` throws if no auth provider is configured
|
|
157
|
-
- `corsOrigins` defaults to `"*"` — use an allowlist in production
|
|
158
|
-
- `maxBodyBytes` defaults to **1 MB** (set `null` to disable)
|
|
159
|
-
- `ipRateLimit` keys on client IP by default
|
|
160
|
-
|
|
161
|
-
---
|
|
162
|
-
|
|
163
|
-
## Features
|
|
164
|
-
|
|
165
|
-
### Middleware Pipeline
|
|
166
|
-
|
|
167
|
-
Composable onion model — same mental model as Hono, Koa, or Express.
|
|
168
|
-
|
|
169
|
-
```typescript
|
|
170
|
-
import type { Middleware } from "@casys/mcp-server";
|
|
171
|
-
|
|
172
|
-
const timing: Middleware = async (ctx, next) => {
|
|
173
|
-
const start = performance.now();
|
|
174
|
-
const result = await next();
|
|
175
|
-
console.log(
|
|
176
|
-
`${ctx.toolName} took ${(performance.now() - start).toFixed(0)}ms`,
|
|
177
|
-
);
|
|
178
|
-
return result;
|
|
179
|
-
};
|
|
180
|
-
|
|
181
|
-
server.use(timing);
|
|
182
|
-
```
|
|
183
|
-
|
|
184
|
-
Built-in pipeline:
|
|
185
|
-
`rate-limit → auth → custom → scope-check → validation → backpressure → handler`
|
|
186
|
-
|
|
187
|
-
### OAuth2 / JWT Auth
|
|
188
|
-
|
|
189
|
-
Four OIDC presets out of the box:
|
|
190
|
-
|
|
191
|
-
```typescript
|
|
192
|
-
import {
|
|
193
|
-
createAuth0AuthProvider, // Auth0
|
|
194
|
-
createGitHubAuthProvider, // GitHub Actions OIDC
|
|
195
|
-
createGoogleAuthProvider, // Google OIDC
|
|
196
|
-
createOIDCAuthProvider, // Generic OIDC (Keycloak, Okta, etc.)
|
|
197
|
-
} from "@casys/mcp-server";
|
|
198
|
-
|
|
199
|
-
const auth0 = createAuth0AuthProvider({
|
|
200
|
-
domain: "my-tenant.auth0.com",
|
|
201
|
-
audience: "https://my-mcp.example.com",
|
|
202
|
-
resource: "https://my-mcp.example.com",
|
|
203
|
-
scopesSupported: ["read", "write"],
|
|
204
|
-
});
|
|
205
|
-
```
|
|
206
|
-
|
|
207
|
-
Or use `JwtAuthProvider` directly for custom setups:
|
|
208
|
-
|
|
209
|
-
```typescript
|
|
210
|
-
import { JwtAuthProvider } from "@casys/mcp-server";
|
|
211
|
-
|
|
212
|
-
const provider = new JwtAuthProvider({
|
|
213
|
-
issuer: "https://my-idp.example.com",
|
|
214
|
-
audience: "https://my-mcp.example.com",
|
|
215
|
-
resource: "https://my-mcp.example.com",
|
|
216
|
-
authorizationServers: ["https://my-idp.example.com"],
|
|
217
|
-
});
|
|
218
|
-
```
|
|
219
|
-
|
|
220
|
-
Token verification is cached (SHA-256 hash → AuthInfo, TTL = min(token expiry,
|
|
221
|
-
5min)) to avoid redundant JWKS round-trips.
|
|
222
|
-
|
|
223
|
-
### YAML + Env Config
|
|
224
|
-
|
|
225
|
-
For binary distribution — users configure auth without code:
|
|
226
|
-
|
|
227
|
-
```yaml
|
|
228
|
-
# mcp-server.yaml
|
|
229
|
-
auth:
|
|
230
|
-
provider: auth0
|
|
231
|
-
audience: https://my-mcp.example.com
|
|
232
|
-
resource: https://my-mcp.example.com
|
|
233
|
-
domain: my-tenant.auth0.com
|
|
234
|
-
scopesSupported: [read, write, admin]
|
|
235
|
-
```
|
|
236
|
-
|
|
237
|
-
Env vars override YAML at deploy time:
|
|
238
|
-
|
|
239
|
-
```bash
|
|
240
|
-
MCP_AUTH_AUDIENCE=https://prod.example.com ./my-server --http --port 3000
|
|
241
|
-
```
|
|
242
|
-
|
|
243
|
-
Priority: `programmatic > env vars > YAML > no auth`
|
|
244
|
-
|
|
245
|
-
### MRTR requestState replay protection
|
|
246
|
-
|
|
247
|
-
When a tool returns `resultType: "input_required"`, configure a signing key so
|
|
248
|
-
the framework can bind the continuation to the principal, method, arguments,
|
|
249
|
-
expiry, and a random nonce:
|
|
250
|
-
|
|
251
|
-
```typescript
|
|
252
|
-
const server = new McpApp({
|
|
253
|
-
name: "my-api",
|
|
254
|
-
version: "1.0.0",
|
|
255
|
-
mrtr: {
|
|
256
|
-
signingKey: Deno.env.get("MCP_MRTR_SIGNING_KEY"),
|
|
257
|
-
},
|
|
258
|
-
});
|
|
259
|
-
```
|
|
260
|
-
|
|
261
|
-
Each verified nonce is consumed before the handler runs. With no explicit
|
|
262
|
-
`replayStore`, the built-in `MemoryMrtrReplayStore` rejects a second use within
|
|
263
|
-
one continuously running process.
|
|
264
|
-
|
|
265
|
-
Multi-instance or restart-safe deployments must inject one durable atomic store
|
|
266
|
-
shared by every instance:
|
|
267
|
-
|
|
268
|
-
```typescript
|
|
269
|
-
import type { MrtrReplayStore } from "@casys/mcp-server";
|
|
270
|
-
|
|
271
|
-
const replayStore: MrtrReplayStore = {
|
|
272
|
-
async consume(nonce, expiresAt) {
|
|
273
|
-
// Atomically reserve the nonce until its signed expiry.
|
|
274
|
-
// Redis equivalent: SET mrtr:<nonce> 1 NX EXAT <expiresAt>
|
|
275
|
-
return await reserveNonce(nonce, expiresAt);
|
|
276
|
-
},
|
|
277
|
-
};
|
|
278
|
-
|
|
279
|
-
const server = new McpApp({
|
|
280
|
-
name: "my-api",
|
|
281
|
-
version: "1.0.0",
|
|
282
|
-
mrtr: {
|
|
283
|
-
signingKey: Deno.env.get("MCP_MRTR_SIGNING_KEY"),
|
|
284
|
-
replayStore,
|
|
285
|
-
},
|
|
286
|
-
});
|
|
287
|
-
```
|
|
288
|
-
|
|
289
|
-
`consume()` must return `true` only for the caller that wins the atomic
|
|
290
|
-
reservation, `false` for a nonce already consumed, and throw when the store is
|
|
291
|
-
unavailable. Store failures are fail-closed; the handler is not executed.
|
|
292
|
-
|
|
293
|
-
This is at-most-once admission, not exactly-once completion. If business logic
|
|
294
|
-
commits and the response is lost, replaying the same token is rejected.
|
|
295
|
-
Returning the prior result safely requires a separate idempotency/result ledger,
|
|
296
|
-
ideally paired with idempotency support in the downstream system.
|
|
297
|
-
|
|
298
|
-
### RFC 9728
|
|
299
|
-
|
|
300
|
-
When auth is configured, the framework automatically exposes
|
|
301
|
-
`GET /.well-known/oauth-protected-resource` per
|
|
302
|
-
[RFC 9728](https://www.rfc-editor.org/rfc/rfc9728).
|
|
303
|
-
|
|
304
|
-
### DCR Discovery Proxy (RFC 8414 + RFC 7591)
|
|
305
|
-
|
|
306
|
-
IdPs without native Dynamic Client Registration (Zitadel, unconfigured Keycloak,
|
|
307
|
-
Okta free tier) don't publish `registration_endpoint` in their AS metadata, so
|
|
308
|
-
MCP clients like Claude.ai or Cursor can't auto-register.
|
|
309
|
-
|
|
310
|
-
`createAsMetadataHandler` is a framework-agnostic Web Standard handler that
|
|
311
|
-
proxies the upstream RFC 8414 metadata and injects a `registration_endpoint`
|
|
312
|
-
pointing to your own DCR proxy:
|
|
313
|
-
|
|
314
|
-
```typescript
|
|
315
|
-
// routes/.well-known/oauth-authorization-server.ts (Fresh example)
|
|
316
|
-
import { createAsMetadataHandler } from "@casys/mcp-server";
|
|
317
|
-
|
|
318
|
-
const handle = createAsMetadataHandler({
|
|
319
|
-
upstreamIssuer: "https://my-tenant.zitadel.cloud",
|
|
320
|
-
registrationEndpoint: "https://my-app.example.com/oauth/register",
|
|
321
|
-
// cacheTtlMs?: 24h default, stale-while-revalidate
|
|
322
|
-
// extraFields?: override scopes_supported, etc.
|
|
323
|
-
});
|
|
324
|
-
|
|
325
|
-
export const handler = { GET: (ctx) => handle(ctx.req) };
|
|
326
|
-
```
|
|
327
|
-
|
|
328
|
-
Then point the PRM at your own host so clients hit the enriched metadata:
|
|
329
|
-
|
|
330
|
-
```typescript
|
|
331
|
-
authorizationServers: ["https://my-app.example.com"],
|
|
332
|
-
```
|
|
333
|
-
|
|
334
|
-
The DCR endpoint itself (RFC 7591 `/oauth/register`) is out of scope — mount it
|
|
335
|
-
in your framework and forward to the IdP's admin API.
|
|
336
|
-
|
|
337
|
-
**Path caveat**: if the MCP server lives at `/mcp`, clients may build the
|
|
338
|
-
discovery URL as `<host>/.well-known/oauth-authorization-server/mcp`. Mount the
|
|
339
|
-
handler at the exact path your PRM advertises.
|
|
340
|
-
|
|
341
|
-
### Observability
|
|
342
|
-
|
|
343
|
-
Every tool call emits an **OpenTelemetry span** with rich attributes:
|
|
344
|
-
|
|
345
|
-
```
|
|
346
|
-
mcp.tool.call query
|
|
347
|
-
mcp.tool.name = "query"
|
|
348
|
-
mcp.server.name = "my-api"
|
|
349
|
-
mcp.transport = "http"
|
|
350
|
-
mcp.session.id = "a1b2c3..."
|
|
351
|
-
mcp.tool.duration_ms = 42
|
|
352
|
-
mcp.tool.success = true
|
|
353
|
-
```
|
|
354
|
-
|
|
355
|
-
Enable with Deno's native OTEL support:
|
|
356
|
-
|
|
357
|
-
```bash
|
|
358
|
-
OTEL_DENO=true deno run --unstable-otel server.ts
|
|
359
|
-
```
|
|
360
|
-
|
|
361
|
-
The HTTP server exposes a **Prometheus-compatible** `/metrics` endpoint:
|
|
362
|
-
|
|
363
|
-
```
|
|
364
|
-
mcp_server_tool_calls_total 1024
|
|
365
|
-
mcp_server_tool_calls_success_total 1018
|
|
366
|
-
mcp_server_tool_calls_failed_total 6
|
|
367
|
-
mcp_server_tool_call_duration_ms_bucket{le="50"} 892
|
|
368
|
-
mcp_server_tool_call_duration_ms_bucket{le="100"} 987
|
|
369
|
-
mcp_server_tool_calls_by_name{tool="query",status="success"} 512
|
|
370
|
-
mcp_server_active_requests 3
|
|
371
|
-
mcp_server_uptime_seconds 86400
|
|
372
|
-
```
|
|
373
|
-
|
|
374
|
-
Programmatic access:
|
|
375
|
-
|
|
376
|
-
```typescript
|
|
377
|
-
server.getServerMetrics(); // Full snapshot (counters, histograms, gauges)
|
|
378
|
-
server.getPrometheusMetrics(); // Prometheus text format string
|
|
379
|
-
```
|
|
380
|
-
|
|
381
|
-
### Concurrency Control
|
|
382
|
-
|
|
383
|
-
Three backpressure strategies when the server is at capacity:
|
|
384
|
-
|
|
385
|
-
| Strategy | Behavior |
|
|
386
|
-
| ----------------- | ------------------------------------------ |
|
|
387
|
-
| `sleep` (default) | Busy-wait with configurable sleep interval |
|
|
388
|
-
| `queue` | FIFO queue with ordered release |
|
|
389
|
-
| `reject` | Fail fast with immediate error |
|
|
390
|
-
|
|
391
|
-
```typescript
|
|
392
|
-
new McpApp({
|
|
393
|
-
maxConcurrent: 10,
|
|
394
|
-
backpressureStrategy: "queue",
|
|
395
|
-
});
|
|
396
|
-
```
|
|
397
|
-
|
|
398
|
-
### Rate Limiting
|
|
399
|
-
|
|
400
|
-
Sliding window rate limiter with per-client tracking:
|
|
401
|
-
|
|
402
|
-
```typescript
|
|
403
|
-
new McpApp({
|
|
404
|
-
rateLimit: {
|
|
405
|
-
maxRequests: 100,
|
|
406
|
-
windowMs: 60_000,
|
|
407
|
-
keyExtractor: (ctx) => ctx.args.clientId as string,
|
|
408
|
-
onLimitExceeded: "wait", // or "reject"
|
|
409
|
-
},
|
|
410
|
-
});
|
|
411
|
-
```
|
|
412
|
-
|
|
413
|
-
For HTTP endpoints, use `startHttp({ ipRateLimit: ... })` to rate limit by
|
|
414
|
-
client IP (or custom key).
|
|
415
|
-
|
|
416
|
-
### Security Best Practices (Tool Handlers)
|
|
417
|
-
|
|
418
|
-
Tool handlers receive **untrusted JSON input**. Treat args as hostile:
|
|
419
|
-
|
|
420
|
-
- **Define strict schemas**: `additionalProperties: false`, `minLength`,
|
|
421
|
-
`pattern`, `enum`.
|
|
422
|
-
- **Never pass raw args to a shell** (`Deno.Command`, `child_process.exec`). If
|
|
423
|
-
you must, use an allowlist + argv array (no shell).
|
|
424
|
-
- **Validate paths & resources**: allowlisted roots, deny `..`, restrict env
|
|
425
|
-
access.
|
|
426
|
-
- **Prefer safe APIs**: parameterized DB queries, SDK methods, typed clients.
|
|
427
|
-
- **Log sensitive actions**: file writes, network calls, admin ops.
|
|
428
|
-
|
|
429
|
-
### MCP Apps (UI Resources)
|
|
430
|
-
|
|
431
|
-
Register interactive UIs as MCP resources:
|
|
432
|
-
|
|
433
|
-
```typescript
|
|
434
|
-
import { MCP_APP_MIME_TYPE, McpApp } from "@casys/mcp-server";
|
|
435
|
-
|
|
436
|
-
server.registerResource(
|
|
437
|
-
{ uri: "ui://my-server/viewer", name: "Data Viewer" },
|
|
438
|
-
async (uri) => ({
|
|
439
|
-
uri: uri.toString(),
|
|
440
|
-
mimeType: MCP_APP_MIME_TYPE,
|
|
441
|
-
text: "<html><body>...</body></html>",
|
|
442
|
-
}),
|
|
443
|
-
);
|
|
444
|
-
```
|
|
445
|
-
|
|
446
|
-
Handlers return one payload form: `text` (including HTML) **or** `blob` for
|
|
447
|
-
binary content encoded as standard padded base64. Existing text handlers remain
|
|
448
|
-
valid. The framework also checks at runtime that the response URI exactly
|
|
449
|
-
matches the requested URI and that the MIME type is non-empty, which protects
|
|
450
|
-
JavaScript and unchecked TypeScript handlers as well as typed callers.
|
|
451
|
-
|
|
452
|
-
```typescript
|
|
453
|
-
server.registerResource(
|
|
454
|
-
{ uri: "file://reports/latest.pdf", name: "Latest report", size: 184_320 },
|
|
455
|
-
async (uri) => ({
|
|
456
|
-
uri: uri.toString(),
|
|
457
|
-
mimeType: "application/pdf",
|
|
458
|
-
blob: await loadReportAsCanonicalBase64(),
|
|
459
|
-
}),
|
|
460
|
-
);
|
|
461
|
-
```
|
|
462
|
-
|
|
463
|
-
`size` is optional resource metadata shown in `resources/list`; when present it
|
|
464
|
-
must be a non-negative safe integer and is verified on every read against the
|
|
465
|
-
exact UTF-8 byte length of `text` or decoded byte length of `blob`. Supplying
|
|
466
|
-
`mimeType` in the resource metadata likewise binds every response to that exact
|
|
467
|
-
MIME type. If it is absent, it is absent from `resources/list`; the handler
|
|
468
|
-
still declares the MIME type when it serves the bytes. Resource content may
|
|
469
|
-
include `_meta`. Put `annotations`, `icons`, `title`, and resource `_meta` on
|
|
470
|
-
the `MCPResource` registration, where MCP defines those fields.
|
|
471
|
-
|
|
472
|
-
With `resourceCsp`, CSP injection applies only to the `text` branch of an HTML
|
|
473
|
-
resource. Blobs are never decoded, transformed, or re-encoded.
|
|
474
|
-
|
|
475
|
-
Register resources before `start()` / `startHttp()` to install the resource
|
|
476
|
-
handlers and advertise `resources: { listChanged: true }`. They can then be
|
|
477
|
-
added or removed at any time through `unregisterResource(uri)`, which returns
|
|
478
|
-
`true` only once. For a registry that starts empty and discovers resources
|
|
479
|
-
asynchronously, construct with `expectResources: true`; that mode installs the
|
|
480
|
-
same handlers at construction time:
|
|
481
|
-
|
|
482
|
-
```typescript
|
|
483
|
-
const app = new McpApp({
|
|
484
|
-
name: "relay",
|
|
485
|
-
version: "1.0.0",
|
|
486
|
-
expectResources: true,
|
|
487
|
-
});
|
|
488
|
-
|
|
489
|
-
// After start(): list/read/templates handlers are already installed.
|
|
490
|
-
app.registerResource(resource, handler);
|
|
491
|
-
app.unregisterResource(resource.uri); // true, then false if called again
|
|
19
|
+
// New code should use:
|
|
20
|
+
import { McpApp } from "@casys/mcp-platform";
|
|
492
21
|
```
|
|
493
|
-
|
|
494
|
-
#### Capability negotiation (clients that don't support MCP Apps)
|
|
495
|
-
|
|
496
|
-
Not every MCP client renders UI resources. Clients that do advertise the
|
|
497
|
-
[MCP Apps extension](https://github.com/modelcontextprotocol/ext-apps) in their
|
|
498
|
-
capabilities (per the SDK 1.29 `extensions` field). Read it from a tool handler
|
|
499
|
-
to decide between rich UI and a text-only fallback:
|
|
500
|
-
|
|
501
|
-
```typescript
|
|
502
|
-
import { MCP_APP_MIME_TYPE, McpApp } from "@casys/mcp-server";
|
|
503
|
-
|
|
504
|
-
const app = new McpApp({ name: "weather-server", version: "1.0.0" });
|
|
505
|
-
|
|
506
|
-
app.registerTool(
|
|
507
|
-
{
|
|
508
|
-
name: "get-weather",
|
|
509
|
-
description: "Get the weather forecast for a city",
|
|
510
|
-
inputSchema: {
|
|
511
|
-
type: "object",
|
|
512
|
-
properties: { city: { type: "string" } },
|
|
513
|
-
required: ["city"],
|
|
514
|
-
},
|
|
515
|
-
},
|
|
516
|
-
async ({ city }) => {
|
|
517
|
-
const forecast = await fetchForecast(city);
|
|
518
|
-
const cap = app.getClientMcpAppsCapability();
|
|
519
|
-
|
|
520
|
-
if (cap?.mimeTypes?.includes(MCP_APP_MIME_TYPE)) {
|
|
521
|
-
// Rich UI: small text summary + interactive resource
|
|
522
|
-
return {
|
|
523
|
-
content: [{ type: "text", text: `Forecast for ${city} loaded` }],
|
|
524
|
-
_meta: { ui: { resourceUri: `ui://weather/${city}` } },
|
|
525
|
-
};
|
|
526
|
-
}
|
|
527
|
-
|
|
528
|
-
// Text-only fallback for clients that can't render the UI
|
|
529
|
-
return {
|
|
530
|
-
content: [{ type: "text", text: formatForecastAsText(forecast) }],
|
|
531
|
-
};
|
|
532
|
-
},
|
|
533
|
-
);
|
|
534
|
-
```
|
|
535
|
-
|
|
536
|
-
`getClientMcpAppsCapability()` returns `undefined` before the client has
|
|
537
|
-
completed its initialize handshake, when the client doesn't advertise MCP Apps
|
|
538
|
-
support, or when the advertised capability is malformed. The standalone
|
|
539
|
-
`getMcpAppsCapability(clientCapabilities)` function is also exported for use
|
|
540
|
-
against arbitrary capability objects.
|
|
541
|
-
|
|
542
|
-
The constants `MCP_APPS_EXTENSION_ID` (`"io.modelcontextprotocol/ui"`) and
|
|
543
|
-
`MCP_APPS_PROTOCOL_VERSION` (`"2026-01-26"`) are exported for agents that need
|
|
544
|
-
to introspect the protocol target directly.
|
|
545
|
-
|
|
546
|
-
---
|
|
547
|
-
|
|
548
|
-
## API Reference
|
|
549
|
-
|
|
550
|
-
### McpApp
|
|
551
|
-
|
|
552
|
-
> **Note:** `ConcurrentMCPServer` and `ConcurrentServerOptions` remain exported
|
|
553
|
-
> as `@deprecated` aliases for backwards compatibility and will be removed in
|
|
554
|
-
> v1.0. New code should use `McpApp` / `McpAppOptions`. The aliases point to the
|
|
555
|
-
> exact same class — `instanceof` checks pass on both.
|
|
556
|
-
|
|
557
|
-
```typescript
|
|
558
|
-
const server = new McpApp(options: McpAppOptions);
|
|
559
|
-
|
|
560
|
-
// Registration (before start, unless expectResources: true)
|
|
561
|
-
server.registerTool(tool, handler);
|
|
562
|
-
server.registerTools(tools, handlers);
|
|
563
|
-
server.registerResource(resource, handler);
|
|
564
|
-
server.registerResources(resources, handlers);
|
|
565
|
-
server.unregisterResource(resourceUri); // safe before or after start
|
|
566
|
-
server.use(middleware);
|
|
567
|
-
|
|
568
|
-
// Transport
|
|
569
|
-
await server.start(); // STDIO
|
|
570
|
-
await server.startHttp({ port: 3000 }); // HTTP + SSE
|
|
571
|
-
await server.stop(); // Graceful shutdown
|
|
572
|
-
|
|
573
|
-
// Observability
|
|
574
|
-
server.getMetrics(); // { inFlight, queued }
|
|
575
|
-
server.getServerMetrics(); // Full snapshot
|
|
576
|
-
server.getPrometheusMetrics(); // Prometheus text format
|
|
577
|
-
server.getRateLimitMetrics(); // { keys, totalRequests }
|
|
578
|
-
|
|
579
|
-
// Introspection
|
|
580
|
-
server.getToolCount();
|
|
581
|
-
server.getToolNames();
|
|
582
|
-
server.getResourceCount();
|
|
583
|
-
server.getResourceUris();
|
|
584
|
-
```
|
|
585
|
-
|
|
586
|
-
### Standalone Components
|
|
587
|
-
|
|
588
|
-
Each component works independently:
|
|
589
|
-
|
|
590
|
-
```typescript
|
|
591
|
-
import { RateLimiter, RequestQueue, SchemaValidator } from "@casys/mcp-server";
|
|
592
|
-
|
|
593
|
-
// Rate limiter
|
|
594
|
-
const limiter = new RateLimiter({ maxRequests: 10, windowMs: 1000 });
|
|
595
|
-
if (limiter.checkLimit("client-123")) {
|
|
596
|
-
/* proceed */
|
|
597
|
-
}
|
|
598
|
-
|
|
599
|
-
// Request queue
|
|
600
|
-
const queue = new RequestQueue({
|
|
601
|
-
maxConcurrent: 5,
|
|
602
|
-
strategy: "queue",
|
|
603
|
-
sleepMs: 10,
|
|
604
|
-
});
|
|
605
|
-
await queue.acquire();
|
|
606
|
-
try {
|
|
607
|
-
/* work */
|
|
608
|
-
} finally {
|
|
609
|
-
queue.release();
|
|
610
|
-
}
|
|
611
|
-
|
|
612
|
-
// Schema validator
|
|
613
|
-
const validator = new SchemaValidator();
|
|
614
|
-
validator.addSchema("tool", {
|
|
615
|
-
type: "object",
|
|
616
|
-
properties: { n: { type: "number" } },
|
|
617
|
-
});
|
|
618
|
-
validator.validate("tool", { n: 5 }); // { valid: true, errors: [] }
|
|
619
|
-
```
|
|
620
|
-
|
|
621
|
-
---
|
|
622
|
-
|
|
623
|
-
## HTTP Endpoints
|
|
624
|
-
|
|
625
|
-
When running with `startHttp()`:
|
|
626
|
-
|
|
627
|
-
| Method | Path | Description |
|
|
628
|
-
| ------ | --------------------------------------- | ----------------------------------------------------------- |
|
|
629
|
-
| `POST` | `/mcp` or `/` | JSON-RPC endpoint (initialize, tools/call, tools/list, ...) |
|
|
630
|
-
| `GET` | `/health` | Health check |
|
|
631
|
-
| `GET` | `/metrics` | Prometheus metrics |
|
|
632
|
-
| `GET` | `/.well-known/oauth-protected-resource` | RFC 9728 metadata (when auth enabled) |
|
|
633
|
-
|
|
634
|
-
---
|
|
635
|
-
|
|
636
|
-
## License
|
|
637
|
-
|
|
638
|
-
MIT
|
package/esm/mod.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "@casys/mcp-platform";
|
package/esm/mod.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "@casys/mcp-platform";
|