@lostgradient/mcp 0.1.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/LICENSE +21 -0
- package/README.md +125 -0
- package/dist/chunk-mf3t0afg.js +4 -0
- package/dist/chunk-psnez3q2.js +111 -0
- package/dist/chunk-taxyscge.js +67 -0
- package/dist/chunk-v14p4gng.js +25 -0
- package/dist/chunk-wmkqd45r.js +14 -0
- package/dist/conformance-fixture-registration.d.ts +30 -0
- package/dist/conformance-server.d.ts +1 -0
- package/dist/env.d.ts +59 -0
- package/dist/env.js +9 -0
- package/dist/environment-schema.d.ts +29 -0
- package/dist/environment-schema.js +6 -0
- package/dist/golden-prompts.d.ts +42 -0
- package/dist/handler-context.d.ts +16 -0
- package/dist/index.d.ts +25 -0
- package/dist/index.js +1326 -0
- package/dist/localhost-request-validation.d.ts +2 -0
- package/dist/logger.d.ts +39 -0
- package/dist/logger.js +14 -0
- package/dist/long-running-operation-support.d.ts +34 -0
- package/dist/metrics.d.ts +64 -0
- package/dist/metrics.js +6 -0
- package/dist/prompts/index.d.ts +5 -0
- package/dist/prompts/summarize.d.ts +4 -0
- package/dist/resources/index.d.ts +4 -0
- package/dist/resources/user-profile.d.ts +1 -0
- package/dist/scope-vocabulary.d.ts +127 -0
- package/dist/scopes.d.ts +49 -0
- package/dist/server.d.ts +113 -0
- package/dist/supported-scopes.d.ts +25 -0
- package/dist/template-registry.d.ts +16 -0
- package/dist/tool-response.d.ts +94 -0
- package/dist/tools/get-user-profile.d.ts +8 -0
- package/dist/tools/index.d.ts +16 -0
- package/dist/tools/list-audit-events.d.ts +11 -0
- package/dist/type-assertions.d.ts +1 -0
- package/dist/types/primitives.d.ts +130 -0
- package/dist/ui-extension-support.d.ts +20 -0
- package/dist/version.d.ts +23 -0
- package/dist/version.js +6 -0
- package/package.json +73 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Steve Kinney
|
|
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
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
# @lostgradient/mcp
|
|
2
|
+
|
|
3
|
+
A [Model Context Protocol](https://modelcontextprotocol.io) server engine: tool, resource, and prompt registries; a consumer-defined OAuth scope vocabulary; and the request-boundary pieces a host needs to serve MCP over HTTP.
|
|
4
|
+
|
|
5
|
+
The engine is deliberately unopinionated about identity and storage. `McpContext` is a plain pre-fetched object with no database handle and no ambient service locator, and `McpUserProfile` is `{ id, email, name, image, role }` — generic, not tied to any identity provider.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm install @lostgradient/mcp
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Requires Node 22 or later, or Bun. The package is ESM-only.
|
|
14
|
+
|
|
15
|
+
## Defining a server
|
|
16
|
+
|
|
17
|
+
Scopes come first, because every tool, resource, and prompt has to declare one. `defineScopes` returns definers bound to your vocabulary, so a mistyped scope is a compile error on the primitive itself rather than a runtime surprise later.
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { defineScopes, createMcpServer } from '@lostgradient/mcp';
|
|
21
|
+
import { z } from 'zod';
|
|
22
|
+
|
|
23
|
+
const scopes = defineScopes({
|
|
24
|
+
'repositories:read': 'Read repository metadata.',
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
const listRepositories = scopes.defineTool({
|
|
28
|
+
name: 'list_repositories',
|
|
29
|
+
title: 'List repositories',
|
|
30
|
+
description: 'Lists repositories the signed-in user can access.',
|
|
31
|
+
inputSchema: z.object({}),
|
|
32
|
+
annotations: {
|
|
33
|
+
readOnlyHint: true,
|
|
34
|
+
destructiveHint: false,
|
|
35
|
+
idempotentHint: true,
|
|
36
|
+
openWorldHint: false,
|
|
37
|
+
},
|
|
38
|
+
requiredScope: 'repositories:read',
|
|
39
|
+
handler: async () => ({ content: [] }),
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
const registry = scopes.defineRegistry({
|
|
43
|
+
tools: [listRepositories],
|
|
44
|
+
resources: [],
|
|
45
|
+
prompts: [],
|
|
46
|
+
serverInfo: { name: 'my-server', version: '1.0.0' },
|
|
47
|
+
// Supply your own. Without this, the server advertises instructions
|
|
48
|
+
// describing a different server's tools and endpoints — see below.
|
|
49
|
+
instructions: 'Lists repositories the signed-in user can access.',
|
|
50
|
+
});
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Constructing a server
|
|
54
|
+
|
|
55
|
+
`createMcpServer` takes a context describing the current request. There is no ambient state: everything it needs is passed in, which is what lets the engine be embedded without knowing how you authenticate or store anything.
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
const server = createMcpServer(
|
|
59
|
+
{
|
|
60
|
+
userId: user.id,
|
|
61
|
+
user, // { id, email, name, image, role }
|
|
62
|
+
scopes: grantedScopes, // the scopes this request's token actually carries
|
|
63
|
+
enableUiExtension: false,
|
|
64
|
+
// Supplied so construction never reads the ambient environment; see
|
|
65
|
+
// "Construction and the ambient environment" below.
|
|
66
|
+
enableConformanceMode: false,
|
|
67
|
+
// Optional: `requestId` to trace one action through your logs, `era` to
|
|
68
|
+
// pin the protocol revision, `publishResourceUpdate` to fan subscription
|
|
69
|
+
// events out across replicas.
|
|
70
|
+
},
|
|
71
|
+
registry,
|
|
72
|
+
);
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
`scopes` is the request's granted scopes, not your vocabulary — the server compares each operation's `requiredScope` against it, so passing the full vocabulary here would authorize everything.
|
|
76
|
+
|
|
77
|
+
> **Always set `instructions` on your registry.** When it is absent, the server falls back to instructions bundled with this package, which describe a different server: its own demo tools, its own OAuth endpoints, and an assertion that every operation is read-only. A client would receive a confident, wrong description of what your server does.
|
|
78
|
+
|
|
79
|
+
### Construction and the ambient environment
|
|
80
|
+
|
|
81
|
+
Importing this package reads nothing from `process.env`. **Constructing a server can**, through two fallbacks, and both are worth supplying explicitly:
|
|
82
|
+
|
|
83
|
+
| Omitted | What happens |
|
|
84
|
+
| ------------------------------- | --------------------------------------------------------- |
|
|
85
|
+
| `context.enableConformanceMode` | falls back to `MCP_CONFORMANCE_MODE` from the environment |
|
|
86
|
+
| `registry.serverInfo` | falls back to `MCP_SERVER_NAME` from the environment |
|
|
87
|
+
|
|
88
|
+
Each fallback calls the environment parser, which requires `NODE_ENV`. So in a plain Node process with `NODE_ENV` unset, omitting **either** makes `createMcpServer` throw before returning — supplying only one is not enough. The example above supplies both, which is why it runs anywhere.
|
|
89
|
+
|
|
90
|
+
If you would rather rely on the environment, set `NODE_ENV` (and any other variable the schema requires) before constructing, and validate it yourself with `parseMcpServerEnvironment` so the failure is yours to report rather than a throw from inside the factory.
|
|
91
|
+
|
|
92
|
+
`getSupportedScopes(registry)` returns the sorted union of every scope your registry actually requires. Use it for the `scopes_supported` field of your OAuth metadata documents and for the `scope` attribute on a `401` challenge, rather than maintaining that list by hand.
|
|
93
|
+
|
|
94
|
+
## Environment
|
|
95
|
+
|
|
96
|
+
Importing this package reads nothing from `process.env` and cannot throw, whatever the ambient environment. That is deliberate: a library which validates its host's environment during module evaluation cannot be embedded, because the host can neither catch the failure nor supply a different environment nor decide whether it wants a server at all.
|
|
97
|
+
|
|
98
|
+
Parse explicitly instead:
|
|
99
|
+
|
|
100
|
+
```ts
|
|
101
|
+
import { parseMcpServerEnvironment, getEnvironment } from '@lostgradient/mcp';
|
|
102
|
+
|
|
103
|
+
const environment = parseMcpServerEnvironment(process.env);
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
`getEnvironment()` is the lazy, memoized equivalent for callers happy to read `process.env` on first use.
|
|
107
|
+
|
|
108
|
+
## Authorizing subscriptions
|
|
109
|
+
|
|
110
|
+
`areResourceSubscriptionsAuthorized(uris, scopes, registry)` is exported because `subscriptions/listen` is decided at the HTTP boundary, before a request reaches the server instance — so a host that mounts this engine must call it there. Omitting it means a client with any valid token can observe updates for a resource whose scope it was never granted.
|
|
111
|
+
|
|
112
|
+
## Subpaths
|
|
113
|
+
|
|
114
|
+
| Subpath | Contents |
|
|
115
|
+
| -------------------------------------- | ---------------------------------------------------------------------- |
|
|
116
|
+
| `@lostgradient/mcp` | The engine: server factory, registries, scope vocabularies, primitives |
|
|
117
|
+
| `@lostgradient/mcp/logger` | The shared pino logger, built lazily on first use |
|
|
118
|
+
| `@lostgradient/mcp/env` | `parseMcpServerEnvironment` and `getEnvironment` |
|
|
119
|
+
| `@lostgradient/mcp/environment-schema` | The raw Zod shape, side-effect free |
|
|
120
|
+
| `@lostgradient/mcp/metrics` | The in-process metrics collector |
|
|
121
|
+
| `@lostgradient/mcp/version` | The advertised package version |
|
|
122
|
+
|
|
123
|
+
## License
|
|
124
|
+
|
|
125
|
+
MIT
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getEnvironment
|
|
3
|
+
} from "./chunk-v14p4gng.js";
|
|
4
|
+
|
|
5
|
+
// src/logger.ts
|
|
6
|
+
import { createRequire } from "node:module";
|
|
7
|
+
import pino from "pino";
|
|
8
|
+
var redactionPaths = [
|
|
9
|
+
"authorization",
|
|
10
|
+
"cookie",
|
|
11
|
+
'["set-cookie"]',
|
|
12
|
+
"token",
|
|
13
|
+
"access_token",
|
|
14
|
+
"refresh_token",
|
|
15
|
+
"id_token",
|
|
16
|
+
"code",
|
|
17
|
+
"code_verifier",
|
|
18
|
+
"code_challenge",
|
|
19
|
+
"client_secret",
|
|
20
|
+
"password",
|
|
21
|
+
"state",
|
|
22
|
+
"topic",
|
|
23
|
+
"email",
|
|
24
|
+
"DATABASE_URL",
|
|
25
|
+
"REDIS_URL",
|
|
26
|
+
"databaseUrl",
|
|
27
|
+
"redisUrl",
|
|
28
|
+
"query",
|
|
29
|
+
"body",
|
|
30
|
+
"formData",
|
|
31
|
+
"headers.authorization",
|
|
32
|
+
"headers.cookie",
|
|
33
|
+
"*.authorization",
|
|
34
|
+
"*.cookie",
|
|
35
|
+
'*["set-cookie"]',
|
|
36
|
+
"*.token",
|
|
37
|
+
"*.access_token",
|
|
38
|
+
"*.refresh_token",
|
|
39
|
+
"*.id_token",
|
|
40
|
+
"*.code",
|
|
41
|
+
"*.code_verifier",
|
|
42
|
+
"*.code_challenge",
|
|
43
|
+
"*.client_secret",
|
|
44
|
+
"*.password",
|
|
45
|
+
"*.state",
|
|
46
|
+
"*.topic",
|
|
47
|
+
"*.email",
|
|
48
|
+
"*.DATABASE_URL",
|
|
49
|
+
"*.REDIS_URL",
|
|
50
|
+
"*.databaseUrl",
|
|
51
|
+
"*.redisUrl",
|
|
52
|
+
"*.query",
|
|
53
|
+
"*.body",
|
|
54
|
+
"*.formData",
|
|
55
|
+
"*.headers.authorization",
|
|
56
|
+
"*.headers.cookie"
|
|
57
|
+
];
|
|
58
|
+
var SECRET_VALUE_PATTERNS = [
|
|
59
|
+
/Bearer\s+[A-Za-z0-9\-._~+/]+=*/gi,
|
|
60
|
+
/eyJ[\w-]{5,}\.[\w-]{5,}\.[\w-]*/g,
|
|
61
|
+
/(?:postgres(?:ql)?|rediss?):\/\/[^:@/\s"]+:[^@/\s"]+@[^\s"]+/gi
|
|
62
|
+
];
|
|
63
|
+
function redactSecretValues(serialized) {
|
|
64
|
+
let result = serialized;
|
|
65
|
+
for (const pattern of SECRET_VALUE_PATTERNS) {
|
|
66
|
+
result = result.replace(pattern, "[REDACTED]");
|
|
67
|
+
}
|
|
68
|
+
return result;
|
|
69
|
+
}
|
|
70
|
+
function createLogger(options) {
|
|
71
|
+
const environment = getEnvironment();
|
|
72
|
+
const baseOptions = {
|
|
73
|
+
level: environment.LOG_LEVEL,
|
|
74
|
+
redact: { paths: [...redactionPaths], censor: "[REDACTED]" },
|
|
75
|
+
hooks: { streamWrite: redactSecretValues }
|
|
76
|
+
};
|
|
77
|
+
if (options?.destination) {
|
|
78
|
+
return pino(baseOptions, options.destination);
|
|
79
|
+
}
|
|
80
|
+
const prettyTransportAvailable = environment.NODE_ENV !== "production" && canResolvePrettyTransport();
|
|
81
|
+
const useWorkerTransport = prettyTransportAvailable && environment.NODE_ENV !== "test";
|
|
82
|
+
return pino({
|
|
83
|
+
...baseOptions,
|
|
84
|
+
...useWorkerTransport && { transport: { target: "pino-pretty" } }
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
function canResolvePrettyTransport() {
|
|
88
|
+
try {
|
|
89
|
+
createRequire(import.meta.url).resolve("pino-pretty");
|
|
90
|
+
return true;
|
|
91
|
+
} catch {
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
var cachedLogger;
|
|
96
|
+
function getLogger() {
|
|
97
|
+
cachedLogger ??= createLogger();
|
|
98
|
+
return cachedLogger;
|
|
99
|
+
}
|
|
100
|
+
var logger = new Proxy({}, {
|
|
101
|
+
get(_target, property, receiver) {
|
|
102
|
+
const real = getLogger();
|
|
103
|
+
return Reflect.get(real, property, receiver === logger ? real : receiver);
|
|
104
|
+
},
|
|
105
|
+
set(_target, property, value, receiver) {
|
|
106
|
+
const real = getLogger();
|
|
107
|
+
return Reflect.set(real, property, value, receiver === logger ? real : receiver);
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
export { redactionPaths, createLogger, getLogger, logger };
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// src/metrics.ts
|
|
2
|
+
var MAX_DURATIONS = 1000;
|
|
3
|
+
function percentile(sorted, p) {
|
|
4
|
+
if (sorted.length === 0)
|
|
5
|
+
return 0;
|
|
6
|
+
const index = Math.ceil(p / 100 * sorted.length) - 1;
|
|
7
|
+
return sorted[Math.max(0, index)];
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
class MetricsCollector {
|
|
11
|
+
tools = new Map;
|
|
12
|
+
events = new Map;
|
|
13
|
+
startedAt = Date.now();
|
|
14
|
+
recordToolInvocation(name, durationMs, isError) {
|
|
15
|
+
let entry = this.tools.get(name);
|
|
16
|
+
if (!entry) {
|
|
17
|
+
entry = { invocations: 0, errors: 0, durations: [] };
|
|
18
|
+
this.tools.set(name, entry);
|
|
19
|
+
}
|
|
20
|
+
entry.invocations++;
|
|
21
|
+
if (isError)
|
|
22
|
+
entry.errors++;
|
|
23
|
+
entry.durations.push(durationMs);
|
|
24
|
+
if (entry.durations.length > MAX_DURATIONS) {
|
|
25
|
+
entry.durations = entry.durations.slice(-MAX_DURATIONS);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
recordEvent(category, outcome) {
|
|
29
|
+
let outcomes = this.events.get(category);
|
|
30
|
+
if (!outcomes) {
|
|
31
|
+
outcomes = new Map;
|
|
32
|
+
this.events.set(category, outcomes);
|
|
33
|
+
}
|
|
34
|
+
outcomes.set(outcome, (outcomes.get(outcome) ?? 0) + 1);
|
|
35
|
+
}
|
|
36
|
+
snapshot() {
|
|
37
|
+
const tools = Object.create(null);
|
|
38
|
+
for (const [name, entry] of this.tools) {
|
|
39
|
+
const sorted = [...entry.durations].sort((a, b) => a - b);
|
|
40
|
+
tools[name] = {
|
|
41
|
+
invocations: entry.invocations,
|
|
42
|
+
errors: entry.errors,
|
|
43
|
+
p50: percentile(sorted, 50),
|
|
44
|
+
p95: percentile(sorted, 95),
|
|
45
|
+
p99: percentile(sorted, 99)
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
const events = {};
|
|
49
|
+
for (const [category, outcomes] of this.events) {
|
|
50
|
+
events[category] = Object.fromEntries(outcomes);
|
|
51
|
+
}
|
|
52
|
+
return {
|
|
53
|
+
tools,
|
|
54
|
+
events,
|
|
55
|
+
uptimeSeconds: Math.floor((Date.now() - this.startedAt) / 1000),
|
|
56
|
+
collectedAt: new Date().toISOString()
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
reset() {
|
|
60
|
+
this.tools.clear();
|
|
61
|
+
this.events.clear();
|
|
62
|
+
this.startedAt = Date.now();
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
var metricsCollector = new MetricsCollector;
|
|
66
|
+
|
|
67
|
+
export { metricsCollector };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import {
|
|
2
|
+
mcpServerEnvironmentSchema
|
|
3
|
+
} from "./chunk-wmkqd45r.js";
|
|
4
|
+
|
|
5
|
+
// src/env.ts
|
|
6
|
+
import { z } from "zod";
|
|
7
|
+
var mcpServerEnvironmentObject = z.object(mcpServerEnvironmentSchema);
|
|
8
|
+
function parseMcpServerEnvironment(env) {
|
|
9
|
+
if (env.SKIP_ENV_VALIDATION) {
|
|
10
|
+
throw new Error("SKIP_ENV_VALIDATION is not supported. Supply a real environment instead — see .env.example.");
|
|
11
|
+
}
|
|
12
|
+
const presentEntries = Object.fromEntries(Object.entries(env).filter((entry) => entry[1] !== undefined && entry[1] !== ""));
|
|
13
|
+
const environment = mcpServerEnvironmentObject.parse(presentEntries);
|
|
14
|
+
if (environment.NODE_ENV === "production" && environment.LOG_CONTENT_DIAGNOSTICS_UNTIL) {
|
|
15
|
+
throw new Error("LOG_CONTENT_DIAGNOSTICS_UNTIL is not supported in production. Raw prompt content logging cannot run in production.");
|
|
16
|
+
}
|
|
17
|
+
return environment;
|
|
18
|
+
}
|
|
19
|
+
var cachedEnvironment;
|
|
20
|
+
function getEnvironment() {
|
|
21
|
+
cachedEnvironment ??= parseMcpServerEnvironment(process.env);
|
|
22
|
+
return cachedEnvironment;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export { parseMcpServerEnvironment, getEnvironment };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// src/environment-schema.ts
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
function strictBooleanEnvironmentFlag(defaultValue) {
|
|
4
|
+
return z.enum(["true", "false"]).optional().default(defaultValue ? "true" : "false").transform((value) => value === "true");
|
|
5
|
+
}
|
|
6
|
+
var mcpServerEnvironmentSchema = {
|
|
7
|
+
MCP_SERVER_NAME: z.string().min(1).optional().default("template-mcp-server"),
|
|
8
|
+
MCP_CONFORMANCE_MODE: strictBooleanEnvironmentFlag(false),
|
|
9
|
+
LOG_LEVEL: z.enum(["fatal", "error", "warn", "info", "debug", "trace"]).optional().default("info"),
|
|
10
|
+
NODE_ENV: z.enum(["development", "production", "test"]),
|
|
11
|
+
LOG_CONTENT_DIAGNOSTICS_UNTIL: z.iso.datetime({ offset: true }).optional()
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export { mcpServerEnvironmentSchema };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { McpServer } from '@modelcontextprotocol/server';
|
|
2
|
+
/**
|
|
3
|
+
* PROTO-002: test-only observability for `test_cancellable_operation`
|
|
4
|
+
* below. A response reaching (or failing to reach) the client proves the
|
|
5
|
+
* HTTP exchange was torn down on abort — the SDK's transport does that
|
|
6
|
+
* regardless of whether a handler's own operation actually observed
|
|
7
|
+
* cancellation. Proving the operation ITSELF stopped (the actual SEC-004
|
|
8
|
+
* bug this wiring fixes: a timeout/abort that only rejects a wrapper
|
|
9
|
+
* promise while the real work keeps running unobserved) needs a side
|
|
10
|
+
* channel outside the response — these counters are it.
|
|
11
|
+
* `mcp-handler.test.ts` asserts `completedCount` stays `0` well past the
|
|
12
|
+
* fixture's full delay once the request was aborted early, which is only
|
|
13
|
+
* true if the underlying `setTimeout` was genuinely cleared, not merely
|
|
14
|
+
* abandoned.
|
|
15
|
+
*/
|
|
16
|
+
export declare const cancellableOperationTestHooks: {
|
|
17
|
+
completedCount: number;
|
|
18
|
+
abortedCount: number;
|
|
19
|
+
reset(): void;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* PROTO-002 / S-11: `resources/subscribe` and `resources/unsubscribe` are
|
|
23
|
+
* registered unconditionally by `server.ts` (spec-compliant ack; real
|
|
24
|
+
* delivery happens on the `subscriptions/listen` stream against a per-user
|
|
25
|
+
* event bus — see that file's comment). This fixture no longer needs its
|
|
26
|
+
* own interest-tracking map: it publishes directly to whatever URI the
|
|
27
|
+
* caller names, and the SDK's own listen-router filters delivery to
|
|
28
|
+
* whichever open streams actually opted into that URI.
|
|
29
|
+
*/
|
|
30
|
+
export declare function registerConformanceFixtures(server: McpServer, publishResourceUpdate?: (uri: string) => Promise<void>): void;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/env.d.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
declare const mcpServerEnvironmentObject: z.ZodObject<{
|
|
3
|
+
MCP_SERVER_NAME: z.ZodDefault<z.ZodOptional<z.ZodString>>;
|
|
4
|
+
MCP_CONFORMANCE_MODE: z.ZodPipe<z.ZodDefault<z.ZodOptional<z.ZodEnum<{
|
|
5
|
+
true: "true";
|
|
6
|
+
false: "false";
|
|
7
|
+
}>>>, z.ZodTransform<boolean, "true" | "false">>;
|
|
8
|
+
LOG_LEVEL: z.ZodDefault<z.ZodOptional<z.ZodEnum<{
|
|
9
|
+
error: "error";
|
|
10
|
+
info: "info";
|
|
11
|
+
fatal: "fatal";
|
|
12
|
+
warn: "warn";
|
|
13
|
+
debug: "debug";
|
|
14
|
+
trace: "trace";
|
|
15
|
+
}>>>;
|
|
16
|
+
NODE_ENV: z.ZodEnum<{
|
|
17
|
+
development: "development";
|
|
18
|
+
production: "production";
|
|
19
|
+
test: "test";
|
|
20
|
+
}>;
|
|
21
|
+
LOG_CONTENT_DIAGNOSTICS_UNTIL: z.ZodOptional<z.ZodISODateTime>;
|
|
22
|
+
}, z.core.$strip>;
|
|
23
|
+
/**
|
|
24
|
+
* Inferred from the parser's own schema object rather than from a
|
|
25
|
+
* separately reconstructed `z.ZodObject<typeof mcpServerEnvironmentSchema>`.
|
|
26
|
+
* The reconstructed form names the shape a second time, so wrapping or
|
|
27
|
+
* refining `mcpServerEnvironmentObject` later would change what
|
|
28
|
+
* `parseMcpServerEnvironment` returns while leaving this type describing
|
|
29
|
+
* the old shape. One source of truth instead.
|
|
30
|
+
*/
|
|
31
|
+
export type McpServerEnvironment = z.infer<typeof mcpServerEnvironmentObject>;
|
|
32
|
+
/**
|
|
33
|
+
* Parses a raw environment record into a validated `McpServerEnvironment`.
|
|
34
|
+
*
|
|
35
|
+
* A function taking the environment as an argument, rather than a
|
|
36
|
+
* module-scope side effect reading `process.env` on import. Importing this
|
|
37
|
+
* package must not impose its environment contract on the host before the
|
|
38
|
+
* host has decided it wants a server at all — a library that throws during
|
|
39
|
+
* module evaluation takes the whole import graph down with it, and a
|
|
40
|
+
* consumer has no way to catch that or to supply a different environment.
|
|
41
|
+
*
|
|
42
|
+
* CONFIG-001 / BUG-001: an empty string is treated as unset. Otherwise a
|
|
43
|
+
* variable that is declared but left blank behaves differently from one
|
|
44
|
+
* never set at all — it bypasses `.default()` and, for a numeric field,
|
|
45
|
+
* coerces to `0` rather than `NaN`. This is why the environment is
|
|
46
|
+
* filtered here rather than handed to Zod directly.
|
|
47
|
+
*/
|
|
48
|
+
export declare function parseMcpServerEnvironment(env: Record<string, string | undefined>): McpServerEnvironment;
|
|
49
|
+
/**
|
|
50
|
+
* Lazily parses and memoizes `process.env` on first access.
|
|
51
|
+
*
|
|
52
|
+
* Every internal caller goes through this rather than through a
|
|
53
|
+
* module-scope constant, so the first read happens when something actually
|
|
54
|
+
* needs a value — not when the module is loaded. Callers that need a fresh
|
|
55
|
+
* read against a mutated `process.env` (tests, primarily) should call
|
|
56
|
+
* `parseMcpServerEnvironment(process.env)` directly.
|
|
57
|
+
*/
|
|
58
|
+
export declare function getEnvironment(): McpServerEnvironment;
|
|
59
|
+
export {};
|
package/dist/env.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
/**
|
|
3
|
+
* The raw Zod shape backing `packages/mcp`'s environment schema, factored
|
|
4
|
+
* out of `env.ts` so it can be introspected without importing `env.ts`
|
|
5
|
+
* itself — see `applications/web/src/environment-schema.ts` for the full
|
|
6
|
+
* rationale (that file and this one exist for the same reason). This file
|
|
7
|
+
* has no side effects: it never reads `process.env` and never throws.
|
|
8
|
+
*/
|
|
9
|
+
export declare const mcpServerEnvironmentSchema: {
|
|
10
|
+
MCP_SERVER_NAME: z.ZodDefault<z.ZodOptional<z.ZodString>>;
|
|
11
|
+
MCP_CONFORMANCE_MODE: z.ZodPipe<z.ZodDefault<z.ZodOptional<z.ZodEnum<{
|
|
12
|
+
true: "true";
|
|
13
|
+
false: "false";
|
|
14
|
+
}>>>, z.ZodTransform<boolean, "true" | "false">>;
|
|
15
|
+
LOG_LEVEL: z.ZodDefault<z.ZodOptional<z.ZodEnum<{
|
|
16
|
+
error: "error";
|
|
17
|
+
info: "info";
|
|
18
|
+
fatal: "fatal";
|
|
19
|
+
warn: "warn";
|
|
20
|
+
debug: "debug";
|
|
21
|
+
trace: "trace";
|
|
22
|
+
}>>>;
|
|
23
|
+
NODE_ENV: z.ZodEnum<{
|
|
24
|
+
development: "development";
|
|
25
|
+
production: "production";
|
|
26
|
+
test: "test";
|
|
27
|
+
}>;
|
|
28
|
+
LOG_CONTENT_DIAGNOSTICS_UNTIL: z.ZodOptional<z.ZodISODateTime>;
|
|
29
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DIST-002: the golden-prompt evaluation set the ChatGPT plugin review
|
|
3
|
+
* process asks for — "a golden-prompt evaluation set covering intended and
|
|
4
|
+
* disallowed tool use, parameter extraction, authentication interruption,
|
|
5
|
+
* and safe handling of untrusted content" (`ROADMAP.local.md`, `DIST-002`).
|
|
6
|
+
*
|
|
7
|
+
* This is a *specification*, not a recorded transcript. Running each prompt
|
|
8
|
+
* against a real ChatGPT developer-mode connection and recording the actual
|
|
9
|
+
* model behavior is the manual step this file cannot perform on its own —
|
|
10
|
+
* ChatGPT is a hosted product with no CLI or scriptable equivalent, and this
|
|
11
|
+
* repository has no live deployment (see `CHATGPT-REVIEW.md`). What this
|
|
12
|
+
* file — and its structural test, `golden-prompts.test.ts` — CAN prove
|
|
13
|
+
* without a live host or a real model: every tool/resource/prompt this set
|
|
14
|
+
* references genuinely exists in the production registry (never the
|
|
15
|
+
* conformance-only `list_audit_events` fixture `CONTENT-001`/`META-001`
|
|
16
|
+
* excluded from it), every scope named is real, and every input parameter
|
|
17
|
+
* named genuinely exists on that operation's own input schema — so the set
|
|
18
|
+
* itself cannot silently drift out of sync with the server it describes.
|
|
19
|
+
*
|
|
20
|
+
* `expectedBehavior` states what a correctly-behaving model and server
|
|
21
|
+
* SHOULD do, in prose a human reviewer (or a future automated harness) can
|
|
22
|
+
* check a real transcript against. It is never a claim that this behavior
|
|
23
|
+
* has been observed.
|
|
24
|
+
*/
|
|
25
|
+
export type GoldenPromptCategory = 'intended-tool-use' | 'disallowed-tool-use' | 'parameter-extraction' | 'authentication-interruption' | 'untrusted-content-handling';
|
|
26
|
+
export interface GoldenPrompt {
|
|
27
|
+
/** Stable id for referencing this case from a review write-up. */
|
|
28
|
+
readonly id: string;
|
|
29
|
+
readonly category: GoldenPromptCategory;
|
|
30
|
+
/** The exact prompt a reviewer types into ChatGPT once connected. */
|
|
31
|
+
readonly prompt: string;
|
|
32
|
+
/**
|
|
33
|
+
* The tool, resource, or prompt name this case exercises, or `null` when
|
|
34
|
+
* the case's whole point is that nothing in the production registry
|
|
35
|
+
* should be reachable for it.
|
|
36
|
+
*/
|
|
37
|
+
readonly operation: string | null;
|
|
38
|
+
/** Real parameter names this prompt should cause the model to extract. */
|
|
39
|
+
readonly expectedParameters: readonly string[];
|
|
40
|
+
readonly expectedBehavior: string;
|
|
41
|
+
}
|
|
42
|
+
export declare const goldenPrompts: readonly GoldenPrompt[];
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
type NotificationSender = (notification: {
|
|
2
|
+
method: string;
|
|
3
|
+
params: Record<string, unknown>;
|
|
4
|
+
}) => Promise<void>;
|
|
5
|
+
type RequestSender = (request: {
|
|
6
|
+
method: string;
|
|
7
|
+
params: Record<string, unknown>;
|
|
8
|
+
}) => Promise<unknown>;
|
|
9
|
+
export declare function readProgressToken(ctx: unknown): string | number | undefined;
|
|
10
|
+
export declare function readSessionIdentifier(ctx: unknown): string | undefined;
|
|
11
|
+
export declare function readNotificationSender(ctx: unknown): NotificationSender | undefined;
|
|
12
|
+
export declare function readRequestSender(ctx: unknown): RequestSender | undefined;
|
|
13
|
+
export declare function stringifyUnknown(value: unknown): string;
|
|
14
|
+
export declare function parseSampledText(result: unknown): string;
|
|
15
|
+
export declare function assertSamplingSupport(ctx: unknown): void;
|
|
16
|
+
export {};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export { createMcpServer, areResourceSubscriptionsAuthorized } from './server.js';
|
|
2
|
+
export { cancellableOperationTestHooks } from './conformance-fixture-registration.js';
|
|
3
|
+
export { getUserProfileTool, listAuditEventsTool, allTools } from './tools/index.js';
|
|
4
|
+
export { userProfileResource, allResources } from './resources/index.js';
|
|
5
|
+
export { summarizePrompt, allPrompts } from './prompts/index.js';
|
|
6
|
+
export { logger, getLogger } from './logger.js';
|
|
7
|
+
export { createToolTextResponse, createToolJsonResponse, createToolStructuredResponse, createToolErrorResponse, } from './tool-response.js';
|
|
8
|
+
export { getEnvironment, parseMcpServerEnvironment } from './env.js';
|
|
9
|
+
export type { McpServerEnvironment } from './env.js';
|
|
10
|
+
export { PACKAGE_VERSION } from './version.js';
|
|
11
|
+
export { hasValidLocalhostRebindingHeaders, isLoopbackHostname, } from './localhost-request-validation.js';
|
|
12
|
+
export declare const EXTENSION_ID = "io.modelcontextprotocol/ui";
|
|
13
|
+
export declare const RESOURCE_MIME_TYPE = "text/html;profile=mcp-app";
|
|
14
|
+
export { readProgressToken, readSessionIdentifier, readNotificationSender, readRequestSender, stringifyUnknown, parseSampledText, assertSamplingSupport, } from './handler-context.js';
|
|
15
|
+
export { metricsCollector } from './metrics.js';
|
|
16
|
+
export type { ToolMetricEntry, MetricsSnapshot } from './metrics.js';
|
|
17
|
+
export { defineTool, defineResource, definePrompt } from './types/primitives.js';
|
|
18
|
+
export type { McpToolDefinition, McpToolAnnotations, McpResourceDefinition, McpPromptDefinition, McpUserProfile, McpContext, } from './types/primitives.js';
|
|
19
|
+
export { mcpScopes, mcpScopeDescriptions, isMcpScope, templateScopeVocabulary } from './scopes.js';
|
|
20
|
+
export { defineScopes } from './scope-vocabulary.js';
|
|
21
|
+
export type { McpScopeVocabulary, McpRegistry } from './scope-vocabulary.js';
|
|
22
|
+
export { templateRegistry } from './template-registry.js';
|
|
23
|
+
export type { McpScope } from './scopes.js';
|
|
24
|
+
export { getSupportedScopes } from './supported-scopes.js';
|
|
25
|
+
export { hasRegisteredUiExtensionResource } from './ui-extension-support.js';
|