@guren/server 2.13.0 → 2.15.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/dist/{Hash-CzrwKeDp.js → Hash-C8aejmi8.js} +5 -0
- package/dist/{McpServiceProvider-D6D9nhgn.js → McpServiceProvider-BawsYjgN.js} +72 -0
- package/dist/agent/AgentApprovalRequested.d.ts +59 -0
- package/dist/agent/approval.d.ts +295 -0
- package/dist/agent/audit-emitter.d.ts +64 -0
- package/dist/agent/audit.d.ts +103 -0
- package/dist/agent/derive.d.ts +155 -0
- package/dist/agent/dispatch.d.ts +165 -0
- package/dist/agent/events.d.ts +80 -0
- package/dist/agent/meta-tools.d.ts +62 -0
- package/dist/agent/public.d.ts +33 -0
- package/dist/agent/public.js +2 -0
- package/dist/agent/redact.d.ts +55 -0
- package/dist/agent/scopes.d.ts +115 -0
- package/dist/auth/AuthManager.d.ts +64 -0
- package/dist/auth/RequestAuthContext.d.ts +3 -5
- package/dist/auth/TokenGuard.d.ts +61 -0
- package/dist/auth/api-token.d.ts +25 -5
- package/dist/auth/context.d.ts +24 -0
- package/dist/auth/index.d.ts +5 -2
- package/dist/auth/index.js +2 -2
- package/dist/auth/password/DefaultHasher.d.ts +18 -5
- package/dist/auth/password/PasswordHasher.d.ts +15 -0
- package/dist/auth/password/hash-format.d.ts +19 -0
- package/dist/auth/providers/UserProvider.d.ts +7 -0
- package/dist/auth/types.d.ts +6 -0
- package/dist/{auth-C7sVmYgc.js → auth-DkYoAPVn.js} +431 -151
- package/dist/authorization/index.js +1 -1
- package/dist/authorization/middleware.d.ts +28 -1
- package/dist/{authorization-Dbcuc5ZK.js → authorization-CdfbCj8J.js} +119 -12
- package/dist/broadcasting/index.js +1 -1
- package/dist/{broadcasting-Dw-FhyjN.js → broadcasting-BwodCA4q.js} +2 -45
- package/dist/container/bindings.d.ts +19 -0
- package/dist/context-DoD0c7J_.js +26 -0
- package/dist/dispatch-erOS9epZ.js +285 -0
- package/dist/encryption/index.js +2 -2
- package/dist/{encryption-DJtkkhiw.js → encryption-D99Vmls3.js} +4 -1
- package/dist/http/dev-assets.d.ts +16 -0
- package/dist/http/middleware/auth.d.ts +2 -2
- package/dist/http/middleware/capabilities.d.ts +57 -0
- package/dist/http/middleware/csrf.d.ts +3 -0
- package/dist/http/public-assets.d.ts +10 -0
- package/dist/http/request.d.ts +139 -3
- package/dist/http/static-documents.d.ts +31 -0
- package/dist/index.d.ts +21 -4
- package/dist/index.js +1302 -118
- package/dist/internal/agent-preflight.d.ts +28 -0
- package/dist/internal/request.d.ts +25 -0
- package/dist/internal/request.js +2 -0
- package/dist/internal/route-path.d.ts +28 -0
- package/dist/internal/route-path.js +33 -0
- package/dist/internal/zod-compat.d.ts +201 -0
- package/dist/internal/zod-compat.js +193 -0
- package/dist/internal/zod-json-schema.d.ts +102 -0
- package/dist/internal/zod-json-schema.js +461 -0
- package/dist/logging/channels/DailyFileChannel.d.ts +0 -2
- package/dist/logging/daily-file-path.d.ts +24 -0
- package/dist/logging/index.d.ts +1 -0
- package/dist/logging/index.js +2 -2
- package/dist/{logging-CPaRMuPP.js → logging-B-kngi3q.js} +61 -18
- package/dist/mcp/create-mcp-server.d.ts +29 -0
- package/dist/mcp/index.js +1 -1
- package/dist/mvc/Controller.d.ts +20 -0
- package/dist/mvc/Router.d.ts +53 -0
- package/dist/request-CVQwpu2J.js +129 -0
- package/dist/runtime/index.js +94 -15
- package/dist/{vite-manifest-gdONBsM3.js → vite-manifest-w_RSOvQW.js} +1 -1
- package/package.json +22 -2
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Turns registered route definitions into agent tools (RFC 0016 §2).
|
|
3
|
+
*
|
|
4
|
+
* This is the *one* derivation. The runtime protocol adapters and the CLI's
|
|
5
|
+
* `.guren/agents.gen.ts` both call it, so a generated manifest and a live MCP
|
|
6
|
+
* server can differ only in description richness — never in schemas, names, or
|
|
7
|
+
* exposure. Nothing here is restated by hand: the input schema comes from the
|
|
8
|
+
* route's own `params`/`query`/`body` contracts through the shared Zod → JSON
|
|
9
|
+
* Schema walker, the output schema from `output`, the ability from the
|
|
10
|
+
* authorization capability its middleware chain stamped.
|
|
11
|
+
*
|
|
12
|
+
* Two properties this function keeps deliberately:
|
|
13
|
+
*
|
|
14
|
+
* - **Total.** Nothing throws and nothing is silently dropped for being
|
|
15
|
+
* malformed. A contract this cannot express becomes a warning plus a
|
|
16
|
+
* deterministic result, because the same call has to serve a dev server, a
|
|
17
|
+
* codegen step, and a production adapter. `guren check` is what turns those
|
|
18
|
+
* warnings into a failing build (PR-1c).
|
|
19
|
+
* - **Opt-in.** A route becomes a tool only by declaring `.agent()` *and*
|
|
20
|
+
* carrying a name. Auto-exposing endpoints is the anti-pattern RFC 0016
|
|
21
|
+
* opens by rejecting.
|
|
22
|
+
*/
|
|
23
|
+
import type { ResourceResponseShape, RouteDefinition } from '../mvc/Router.js';
|
|
24
|
+
import { type JsonSchemaObject } from '../internal/zod-json-schema.js';
|
|
25
|
+
/**
|
|
26
|
+
* A JSON Schema 2020-12 object as an agent tool advertises it. An alias of the
|
|
27
|
+
* shared walker's type rather than a second definition — OpenAPI 3.1's Schema
|
|
28
|
+
* Object is the same dialect, and `@guren/openapi` aliases it the same way, so
|
|
29
|
+
* one definition serves every surface. The name exists so the internal module
|
|
30
|
+
* is not the one a consumer has to import.
|
|
31
|
+
*/
|
|
32
|
+
export type AgentToolSchema = JsonSchemaObject;
|
|
33
|
+
/**
|
|
34
|
+
* MCP `ToolAnnotations`, resolved to explicit values — a consumer never has to
|
|
35
|
+
* re-apply a default, and `guren check`'s annotation-honesty rule has one
|
|
36
|
+
* concrete claim to check the controller against.
|
|
37
|
+
*
|
|
38
|
+
* These are hints for client UX, not enforcement. Authorization lives in
|
|
39
|
+
* policies, scopes, and the approval queue.
|
|
40
|
+
*/
|
|
41
|
+
export interface DerivedAgentToolAnnotations {
|
|
42
|
+
/** The tool does not modify its environment. */
|
|
43
|
+
readOnlyHint: boolean;
|
|
44
|
+
/** The tool may perform destructive updates. Meaningful only when not read-only. */
|
|
45
|
+
destructiveHint: boolean;
|
|
46
|
+
/** Repeat calls with the same arguments have no additional effect. */
|
|
47
|
+
idempotentHint: boolean;
|
|
48
|
+
}
|
|
49
|
+
/** Protocol surfaces a tool appears on. Both resolved; unset means exposed. */
|
|
50
|
+
export interface DerivedAgentToolExposure {
|
|
51
|
+
mcp: boolean;
|
|
52
|
+
webMcp: boolean;
|
|
53
|
+
}
|
|
54
|
+
export interface DerivedAgentTool {
|
|
55
|
+
/** Tool identity: `agent.toolName` when given, otherwise the route name, verbatim. */
|
|
56
|
+
toolName: string;
|
|
57
|
+
/** The route name this tool was derived from. Equal to `toolName` unless overridden. */
|
|
58
|
+
routeName: string;
|
|
59
|
+
/** Uppercased HTTP method the tool dispatches to. */
|
|
60
|
+
method: string;
|
|
61
|
+
/** Route path pattern, `:param` tokens intact. */
|
|
62
|
+
path: string;
|
|
63
|
+
/**
|
|
64
|
+
* `agent.description`, else the route's OpenAPI `description`, else its
|
|
65
|
+
* `summary`. Absent when the route declares none — nothing is invented here;
|
|
66
|
+
* the CLI appends resource type text at codegen time, and `guren check`
|
|
67
|
+
* warns about a tool with no description at all.
|
|
68
|
+
*/
|
|
69
|
+
description?: string;
|
|
70
|
+
/** Merged `params` + `query` + `body`, object root (MCP requires one). */
|
|
71
|
+
inputSchema: AgentToolSchema;
|
|
72
|
+
/**
|
|
73
|
+
* Which contract each advertised input property came from — the inverse of
|
|
74
|
+
* the merge, for the adapter that has to take a flat tool call apart again
|
|
75
|
+
* and rebuild the HTTP request: `params`/`path` substitute into the URL
|
|
76
|
+
* path, `query` joins the query string, `body` lands in the JSON body. The
|
|
77
|
+
* schema alone cannot answer this (the merge is flat by design), and an
|
|
78
|
+
* adapter guessing by method would put a POST route's `query` keys in the
|
|
79
|
+
* body, where `validateQuery` never looks.
|
|
80
|
+
*/
|
|
81
|
+
inputSources: Record<string, AgentToolInputSource>;
|
|
82
|
+
/**
|
|
83
|
+
* True when the route's `body` schema was not an object and therefore nests
|
|
84
|
+
* under a single `body` property (see {@link buildInputSchema}). The
|
|
85
|
+
* dispatching adapter must then send `arguments.body` *as* the HTTP body,
|
|
86
|
+
* not an object wrapping it — the flat reconstruction would post
|
|
87
|
+
* `{ body: [...] }` to a route that validates an array.
|
|
88
|
+
*/
|
|
89
|
+
inputBodyNested: boolean;
|
|
90
|
+
/** Present only when the route binds an `output` schema — the one shape validated at runtime. */
|
|
91
|
+
outputSchema?: AgentToolSchema;
|
|
92
|
+
/**
|
|
93
|
+
* The route's `resource` response hint, as class names — RFC 0016's second
|
|
94
|
+
* output rung, carried rather than resolved. `definitions()` has only the
|
|
95
|
+
* names; the payload type behind them exists solely in the CLI's AST
|
|
96
|
+
* extraction, which is why codegen enriches the description from this and
|
|
97
|
+
* the runtime does not.
|
|
98
|
+
*
|
|
99
|
+
* Absent when the route declares no hint — and, deliberately, whenever the
|
|
100
|
+
* route *declares* an `output` schema, whether or not the walker could
|
|
101
|
+
* render it. The declaration is what the runtime validates against, so it
|
|
102
|
+
* outranks the hint; carrying both would leave two descriptions of one
|
|
103
|
+
* response with nothing keeping them in agreement, and a route whose
|
|
104
|
+
* `output` the walker cannot express would advertise the *unvalidated* one
|
|
105
|
+
* while the enforced contract silently vanished.
|
|
106
|
+
*/
|
|
107
|
+
resource?: ResourceResponseShape;
|
|
108
|
+
annotations: DerivedAgentToolAnnotations;
|
|
109
|
+
/**
|
|
110
|
+
* The policy ability guarding this route, when its middleware chain stamped
|
|
111
|
+
* one unambiguously. Absent means "not statically derivable", never "not
|
|
112
|
+
* authorized" — a check reads the absence, it does not assume a verdict.
|
|
113
|
+
*/
|
|
114
|
+
authorization?: {
|
|
115
|
+
ability: string;
|
|
116
|
+
};
|
|
117
|
+
/** Declared verbatim: invocations require server-side approval. */
|
|
118
|
+
approval?: 'required';
|
|
119
|
+
/** Declared verbatim: argument fields masked in agent audit logs. */
|
|
120
|
+
redact?: string[];
|
|
121
|
+
expose: DerivedAgentToolExposure;
|
|
122
|
+
/**
|
|
123
|
+
* What could not be expressed while deriving *this* tool, unprefixed. The
|
|
124
|
+
* same lines appear in {@link DeriveAgentToolsResult.warnings} prefixed with
|
|
125
|
+
* the tool name; that aggregate is for printing, this field is for anything
|
|
126
|
+
* that needs to know which tool a warning belongs to. Attributing by parsing
|
|
127
|
+
* the prefix back off would couple every consumer to a message format
|
|
128
|
+
* nothing states. Empty for a tool that derived cleanly.
|
|
129
|
+
*/
|
|
130
|
+
warnings: string[];
|
|
131
|
+
}
|
|
132
|
+
export interface DeriveAgentToolsResult {
|
|
133
|
+
tools: DerivedAgentTool[];
|
|
134
|
+
/**
|
|
135
|
+
* Everything the derivation could not express, one line each, prefixed with
|
|
136
|
+
* the tool (or the route, for a route that could not become one). Non-fatal
|
|
137
|
+
* by construction — see the module header. Per-tool lines are also on the
|
|
138
|
+
* tool itself ({@link DerivedAgentTool.warnings}), which is what a consumer
|
|
139
|
+
* needing attribution should read.
|
|
140
|
+
*/
|
|
141
|
+
warnings: string[];
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Where a merged input property came from — collision reporting inside the
|
|
145
|
+
* merge, request reconstruction outside it ({@link DerivedAgentTool.inputSources}).
|
|
146
|
+
* `params` and `path` both mean URL substitution to an adapter; they stay
|
|
147
|
+
* distinct because a warning should name the schema the author actually wrote.
|
|
148
|
+
*/
|
|
149
|
+
export type AgentToolInputSource = 'params' | 'path' | 'query' | 'body';
|
|
150
|
+
/**
|
|
151
|
+
* Derive the agent tools a set of route definitions exposes.
|
|
152
|
+
*
|
|
153
|
+
* @param definitions Registered routes, as `Router.definitions()` hands them out.
|
|
154
|
+
*/
|
|
155
|
+
export declare function deriveAgentTools(definitions: RouteDefinition[]): DeriveAgentToolsResult;
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The dispatch contract (RFC 0016 §3): a tool call re-enters the application
|
|
3
|
+
* as a real HTTP request, so validation, policies, and middleware run exactly
|
|
4
|
+
* once, in the app — never re-implemented here.
|
|
5
|
+
*
|
|
6
|
+
* This module is pure request/response plumbing: it rebuilds an HTTP request
|
|
7
|
+
* from a flat tool call and maps the app's response onto an MCP tool result.
|
|
8
|
+
* It never validates arguments (the route does, and the MCP client already
|
|
9
|
+
* saw the JSON Schema) and never decides authorization (the gate and the
|
|
10
|
+
* app's policies do).
|
|
11
|
+
*/
|
|
12
|
+
import type { DerivedAgentTool } from './derive.js';
|
|
13
|
+
import type { AgentSurface } from './events.js';
|
|
14
|
+
export interface BuildToolRequestOptions {
|
|
15
|
+
/**
|
|
16
|
+
* Origin the synthesized URL is built on. Never leaves the process — the
|
|
17
|
+
* request goes straight into `app.fetch` — but host-authorization
|
|
18
|
+
* middleware still reads its `Host`, so the default stays on the one name
|
|
19
|
+
* every dev allowlist admits.
|
|
20
|
+
*/
|
|
21
|
+
origin?: string;
|
|
22
|
+
/**
|
|
23
|
+
* `Authorization` header to forward verbatim, so the app's own token guard
|
|
24
|
+
* authenticates the same principal the adapter verified. Absent for a
|
|
25
|
+
* surface that authenticates some other way.
|
|
26
|
+
*/
|
|
27
|
+
authorization?: string;
|
|
28
|
+
/**
|
|
29
|
+
* Ask for a verdict instead of an execution (RFC 0016 §5.4): the request
|
|
30
|
+
* runs the route's middleware and validates the advertised contract, then
|
|
31
|
+
* stops before the handler. Only routes declaring `.agent()` honour it.
|
|
32
|
+
*/
|
|
33
|
+
preflight?: boolean;
|
|
34
|
+
/**
|
|
35
|
+
* Which protocol surface the call arrived on, announced to the application
|
|
36
|
+
* as `X-Guren-Agent-Surface`. Defaults to `'mcp'`, the surface that was the
|
|
37
|
+
* only one when this header was introduced, so every existing caller keeps
|
|
38
|
+
* sending exactly what it sent before.
|
|
39
|
+
*
|
|
40
|
+
* The header is informational and, in the framework, write-only: nothing
|
|
41
|
+
* here reads it back. It is there for an *application* that wants to tell
|
|
42
|
+
* an in-browser session call from a bearer-token one, and it borrows the
|
|
43
|
+
* audit trail's vocabulary ({@link AgentSurface}) so the two agree — but
|
|
44
|
+
* the trail's own `surface` comes from the adapter that recorded it, not
|
|
45
|
+
* from this header. Nothing must ever authorize on it: any client sets any
|
|
46
|
+
* header it likes, so a surface can never relax a check.
|
|
47
|
+
*/
|
|
48
|
+
surface?: AgentSurface;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* The argument-level spelling of a preflight request (RFC 0016 §5.4), for a
|
|
52
|
+
* surface whose callers pass flat arguments rather than dispatch options.
|
|
53
|
+
*
|
|
54
|
+
* No adapter strips it today, because none accepts it. Every surface that
|
|
55
|
+
* reaches the seam asks through `BuildToolRequestOptions.preflight` instead:
|
|
56
|
+
* `guren tool:call` and `@guren/testing` from their own flags, and MCP from
|
|
57
|
+
* the `guren.preflight` companion tool, which takes the target tool's name
|
|
58
|
+
* and arguments rather than smuggling a directive through them (RFC 0016
|
|
59
|
+
* §5.4 — a verdict conforms to no route's output schema, so it needs a tool
|
|
60
|
+
* of its own). A surface that adopts this key owns the stripping — it is an
|
|
61
|
+
* instruction to the adapter, not a field of any route's contract, and
|
|
62
|
+
* forwarding it would fail the very validation the caller asked to rehearse.
|
|
63
|
+
*/
|
|
64
|
+
export declare const PREFLIGHT_ARGUMENT = "_preflight";
|
|
65
|
+
export type BuiltToolRequest = {
|
|
66
|
+
request: Request;
|
|
67
|
+
} | ToolRequestBuildFailure;
|
|
68
|
+
/** The ways `buildToolRequest` refuses to build — no HTTP has happened. */
|
|
69
|
+
export type ToolRequestBuildFailure =
|
|
70
|
+
/** Path parameters the call did not supply — the URL cannot be built. */
|
|
71
|
+
{
|
|
72
|
+
missing: string[];
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Path parameter values that are URL dot-segments (`.` / `..`). Rejected,
|
|
76
|
+
* never substituted: `encodeURIComponent` leaves a dot untouched, and the
|
|
77
|
+
* `Request` URL parser then collapses the segment — so `{ name: '..' }` on
|
|
78
|
+
* `/files/:name/meta` would reach `/meta`, a route the scope check never
|
|
79
|
+
* saw. The only ASCII segments the WHATWG parser normalizes are `.` and
|
|
80
|
+
* `..`; every other separator (`/`, `\`) survives `encodeURIComponent`
|
|
81
|
+
* percent-encoded, so this is the complete set.
|
|
82
|
+
*/
|
|
83
|
+
| {
|
|
84
|
+
invalidPath: string[];
|
|
85
|
+
};
|
|
86
|
+
/**
|
|
87
|
+
* The agent-facing diagnosis of a {@link ToolRequestBuildFailure}.
|
|
88
|
+
*
|
|
89
|
+
* One function rather than a string per adapter: the same tool is reachable
|
|
90
|
+
* from several surfaces (App MCP, WebMCP, `guren tool:call`), and an agent
|
|
91
|
+
* that reads a different diagnosis depending on which one it reached would
|
|
92
|
+
* be debugging the client instead of its own call.
|
|
93
|
+
*/
|
|
94
|
+
export declare function describeBuildFailure(failure: ToolRequestBuildFailure): string;
|
|
95
|
+
/**
|
|
96
|
+
* Rebuild the HTTP request a tool call describes.
|
|
97
|
+
*
|
|
98
|
+
* The flat argument object is taken apart along `tool.inputSources` — the
|
|
99
|
+
* derivation's own record of which contract each property came from — so a
|
|
100
|
+
* POST route's `query` keys land in the query string where `validateQuery`
|
|
101
|
+
* reads them, not in the body. Keys the derivation never advertised are
|
|
102
|
+
* forwarded anyway (body for body-carrying methods, query otherwise): the
|
|
103
|
+
* route's validator is the one place that rejects them, and dropping them
|
|
104
|
+
* here would make this a second, quieter validator.
|
|
105
|
+
*
|
|
106
|
+
* GET and HEAD force everything into the query string whatever its source —
|
|
107
|
+
* `Request` refuses a body on those methods, and a `body` schema on a GET
|
|
108
|
+
* route is a contract defect this adapter cannot repair.
|
|
109
|
+
*/
|
|
110
|
+
export declare function buildToolRequest(tool: DerivedAgentTool, args: Record<string, unknown>, options?: BuildToolRequestOptions): BuiltToolRequest;
|
|
111
|
+
/** An MCP tool result, shaped for the SDK's CallToolResult. */
|
|
112
|
+
export interface ToolCallOutcome {
|
|
113
|
+
content: Array<{
|
|
114
|
+
type: 'text';
|
|
115
|
+
text: string;
|
|
116
|
+
}>;
|
|
117
|
+
structuredContent?: Record<string, unknown>;
|
|
118
|
+
isError?: boolean;
|
|
119
|
+
/** The HTTP status the dispatch resolved to, for the audit event. */
|
|
120
|
+
status: number;
|
|
121
|
+
/**
|
|
122
|
+
* The response carried the preflight verdict header, so this is the seam's
|
|
123
|
+
* answer and not the route's own output.
|
|
124
|
+
*
|
|
125
|
+
* Carried here because `mapToolResponse` is the only place that sees the
|
|
126
|
+
* header — an outcome has no headers — and the fact is otherwise lost. A
|
|
127
|
+
* caller that re-derived it by looking for a `preflight` field in the body
|
|
128
|
+
* would read an ordinary route whose output happens to carry that field as a
|
|
129
|
+
* rehearsal that never ran, which is the exact confusion the header was
|
|
130
|
+
* introduced to prevent.
|
|
131
|
+
*/
|
|
132
|
+
preflightVerdict?: boolean;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Whether the tool advertises an object output schema — the only kind MCP
|
|
136
|
+
* lists, and therefore the only kind that obliges a success result to carry
|
|
137
|
+
* `structuredContent`. A route whose `output` is an array or primitive is not
|
|
138
|
+
* advertised as structured (MCP `outputSchema` must be an object), so its
|
|
139
|
+
* results ride as text. `describeTool` and `mapToolResponse` must agree on
|
|
140
|
+
* this exact predicate, or one advertises a schema the other cannot satisfy.
|
|
141
|
+
*/
|
|
142
|
+
export declare function advertisesStructuredOutput(tool: DerivedAgentTool): boolean;
|
|
143
|
+
/**
|
|
144
|
+
* Map the application's response onto an MCP tool result (RFC 0016 §3.4).
|
|
145
|
+
*
|
|
146
|
+
* - 2xx JSON object → serialized text, plus `structuredContent` when the tool
|
|
147
|
+
* advertises an object `outputSchema` (the one shape the route validated).
|
|
148
|
+
* - 2xx Inertia page JSON → unwrapped to `page.props`, only for a tool with
|
|
149
|
+
* no `outputSchema` — unwrap and schema are mutually exclusive by
|
|
150
|
+
* derivation, so the advertised shape can never disagree with the result.
|
|
151
|
+
* - 204 / 3xx → a status line naming the Location; not an error.
|
|
152
|
+
* - 4xx / 5xx → `isError: true` carrying the exception handler's JSON body:
|
|
153
|
+
* a 422's `{ message, errors }` is an application-level failure the agent
|
|
154
|
+
* should read, not a protocol error.
|
|
155
|
+
* - non-JSON → capped text.
|
|
156
|
+
*
|
|
157
|
+
* One MCP invariant overrides the table: a *non-error* result for a tool that
|
|
158
|
+
* advertises an object `outputSchema` must carry `structuredContent`, or the
|
|
159
|
+
* SDK client rejects it after the route has already run. A success response
|
|
160
|
+
* that yields no object to put there (204, 3xx, non-JSON, a non-object body)
|
|
161
|
+
* contradicts the route's own declared output, so it becomes an `isError`
|
|
162
|
+
* result naming the mismatch — which the SDK exempts from the rule — rather
|
|
163
|
+
* than a protocol fault the agent cannot interpret.
|
|
164
|
+
*/
|
|
165
|
+
export declare function mapToolResponse(tool: DerivedAgentTool, response: Response): Promise<ToolCallOutcome>;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The audit trail of the agent surface (RFC 0016 §5.2): every tool invocation
|
|
3
|
+
* and every denial, as framework events, so an application forwards them
|
|
4
|
+
* wherever it already forwards events instead of learning a second logging
|
|
5
|
+
* API.
|
|
6
|
+
*
|
|
7
|
+
* Declarations only. Nothing here emits — the dispatch path does, once, at
|
|
8
|
+
* the one place that knows the verdict — and nothing here redacts:
|
|
9
|
+
* {@link AgentToolInvoked.arguments} is a *contract* that the emitter has
|
|
10
|
+
* already run the arguments through `redactAgentArguments`. An event class
|
|
11
|
+
* that redacted on construction would give a second, quieter redaction rule
|
|
12
|
+
* beside the real one, and a listener has no way to tell a masked payload
|
|
13
|
+
* from an unmasked one.
|
|
14
|
+
*/
|
|
15
|
+
import { Event } from '../events/Event.js';
|
|
16
|
+
/**
|
|
17
|
+
* Who invoked a tool (RFC 0016 §4). `kind: 'service'` is an App MCP bearer
|
|
18
|
+
* token with no `userId`; `'user'` is a session (WebMCP) or a user-owned
|
|
19
|
+
* token. `abilities` are the token's, carried for the audit record — the
|
|
20
|
+
* scope verdict itself was already taken by the dispatcher.
|
|
21
|
+
*/
|
|
22
|
+
export interface AgentPrincipal {
|
|
23
|
+
kind: 'user' | 'service';
|
|
24
|
+
id: string | number;
|
|
25
|
+
abilities?: string[];
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* The protocol surface an invocation arrived on. `'mcp'` is the application's
|
|
29
|
+
* own MCP endpoint, `'dev-mcp'` the development one, `'cli'` a
|
|
30
|
+
* `guren tool:call`, `'webmcp'` an in-browser session-authenticated call.
|
|
31
|
+
*/
|
|
32
|
+
export type AgentSurface = 'mcp' | 'dev-mcp' | 'cli' | 'webmcp';
|
|
33
|
+
/**
|
|
34
|
+
* Emitted when an agent tool invocation completed — successfully or not.
|
|
35
|
+
* `status` carries the HTTP status the dispatch resolved to, so an error
|
|
36
|
+
* response is one of these rather than a separate event; a call that never
|
|
37
|
+
* reached the handler is an {@link AgentToolDenied} instead.
|
|
38
|
+
*/
|
|
39
|
+
export declare class AgentToolInvoked extends Event {
|
|
40
|
+
readonly principal: AgentPrincipal | null;
|
|
41
|
+
readonly tool: string;
|
|
42
|
+
readonly status: number;
|
|
43
|
+
readonly durationMs: number;
|
|
44
|
+
readonly surface: AgentSurface;
|
|
45
|
+
/**
|
|
46
|
+
* The invocation arguments **already redacted** by the emitter (see the
|
|
47
|
+
* module header). Assigned in the body rather than declared as a
|
|
48
|
+
* constructor parameter property: `arguments` is not a legal binding
|
|
49
|
+
* identifier in strict mode, which every ES module is, though it is a
|
|
50
|
+
* perfectly legal property name.
|
|
51
|
+
*/
|
|
52
|
+
readonly arguments: Record<string, unknown>;
|
|
53
|
+
constructor(principal: AgentPrincipal | null, tool: string, redactedArguments: Record<string, unknown>, status: number, durationMs: number, surface: AgentSurface);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Why a tool invocation was refused before it reached the route handler.
|
|
57
|
+
*
|
|
58
|
+
* Deliberately no `'policy'`: `Gate` policies evaluate *inside* the
|
|
59
|
+
* dispatched request, so a policy denial is an execution that returned 403 —
|
|
60
|
+
* an {@link AgentToolInvoked} with that status — not an adapter-level
|
|
61
|
+
* refusal. The reasons here are exactly the checks the adapter runs before
|
|
62
|
+
* synthesizing the request, which is also why a denial carries no HTTP
|
|
63
|
+
* status: no HTTP happened.
|
|
64
|
+
*/
|
|
65
|
+
export type AgentToolDenialReason = 'auth' | 'scope' | 'approval' | 'rate-limit';
|
|
66
|
+
/**
|
|
67
|
+
* Emitted when an agent tool invocation was refused. Carries no status or
|
|
68
|
+
* duration: nothing ran, and {@link AgentToolDenialReason} is the finding.
|
|
69
|
+
* `'approval'` covers a tool declaring `approval: 'required'` whose call
|
|
70
|
+
* became a pending record instead of an execution.
|
|
71
|
+
*/
|
|
72
|
+
export declare class AgentToolDenied extends Event {
|
|
73
|
+
readonly principal: AgentPrincipal | null;
|
|
74
|
+
readonly tool: string;
|
|
75
|
+
readonly reason: AgentToolDenialReason;
|
|
76
|
+
readonly surface: AgentSurface;
|
|
77
|
+
/** Redacted by the emitter, exactly as on {@link AgentToolInvoked}. */
|
|
78
|
+
readonly arguments: Record<string, unknown>;
|
|
79
|
+
constructor(principal: AgentPrincipal | null, tool: string, redactedArguments: Record<string, unknown>, reason: AgentToolDenialReason, surface: AgentSurface);
|
|
80
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The names a protocol adapter may add to an application's tool catalogue on
|
|
3
|
+
* its own, and which an application route therefore may not claim (RFC 0016
|
|
4
|
+
* §5.4).
|
|
5
|
+
*
|
|
6
|
+
* This is the one rule, with two readers. `@guren/plugin-mcp` adds
|
|
7
|
+
* `guren.preflight` to the catalogue it serves; `guren check` fails a route
|
|
8
|
+
* whose `.agent()` tool name would collide with it. Restating the string in
|
|
9
|
+
* either place is how the two drift: the check would keep passing a route
|
|
10
|
+
* whose tool the endpoint has already shadowed, and `tools/list` would carry
|
|
11
|
+
* two tools with one name — which an MCP client rejects wholesale, so the
|
|
12
|
+
* collision costs the *entire* catalogue, not the one tool.
|
|
13
|
+
*
|
|
14
|
+
* It lives beside the derivation rather than in the plugin because the check
|
|
15
|
+
* cannot import the plugin (the CLI does not depend on it, and an app that
|
|
16
|
+
* never installs App MCP is still checked), and the plugin must not restate a
|
|
17
|
+
* name the check is enforcing.
|
|
18
|
+
*
|
|
19
|
+
* The list stays deliberately short. Every entry is a name taken away from
|
|
20
|
+
* applications, and the `guren.` prefix is not reserved wholesale for the
|
|
21
|
+
* same reason: reserving a namespace nothing occupies fails routes over a
|
|
22
|
+
* collision that does not exist.
|
|
23
|
+
*/
|
|
24
|
+
/**
|
|
25
|
+
* The preflight companion tool (RFC 0016 §5.4).
|
|
26
|
+
*
|
|
27
|
+
* Preflight cannot be an *argument* of the tool being checked on MCP: a tool
|
|
28
|
+
* advertising an `outputSchema` must answer with `structuredContent`
|
|
29
|
+
* conforming to it unless the result is an error, and a verdict conforms to
|
|
30
|
+
* no route's output. So the verdict needs a tool of its own, with its own
|
|
31
|
+
* output schema — one meta-tool for the whole catalogue, not one companion
|
|
32
|
+
* per tool, which would double the catalogue against §5.5's own
|
|
33
|
+
* catalogue-quality rule.
|
|
34
|
+
*/
|
|
35
|
+
export declare const PREFLIGHT_TOOL_NAME = "guren.preflight";
|
|
36
|
+
/**
|
|
37
|
+
* The approval-status companion tool (RFC 0016 §5.4 item 4).
|
|
38
|
+
*
|
|
39
|
+
* A tool declaring `approval: 'required'` answers its first call with a
|
|
40
|
+
* pending record rather than an execution, and the caller is handed a request
|
|
41
|
+
* id. Something has to be able to say what became of that id, and it cannot be
|
|
42
|
+
* the gated tool itself for the reason preflight cannot be an argument of one:
|
|
43
|
+
* a status is not that route's output, and a tool advertising an `outputSchema`
|
|
44
|
+
* may not answer with a different shape of success.
|
|
45
|
+
*
|
|
46
|
+
* A second meta-tool rather than a mode of the first: `guren.preflight`
|
|
47
|
+
* rehearses a call and answers a verdict, this reads a record and answers its
|
|
48
|
+
* state. Folding both under one name would mean one tool whose output schema
|
|
49
|
+
* is the union of two unrelated shapes, which is the thing a schema is for
|
|
50
|
+
* preventing.
|
|
51
|
+
*/
|
|
52
|
+
export declare const APPROVAL_STATUS_TOOL_NAME = "guren.approval_status";
|
|
53
|
+
/** Every meta-tool name an adapter may occupy. */
|
|
54
|
+
export declare const RESERVED_AGENT_TOOL_NAMES: readonly string[];
|
|
55
|
+
/**
|
|
56
|
+
* Whether a tool name belongs to the framework rather than the application.
|
|
57
|
+
*
|
|
58
|
+
* Case-sensitive, like every other comparison the tool-name grammar makes:
|
|
59
|
+
* MCP tool names are matched literally, so `Guren.Preflight` is a different
|
|
60
|
+
* tool and reserving it would take a name away for nothing.
|
|
61
|
+
*/
|
|
62
|
+
export declare function isReservedAgentToolName(name: string): boolean;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The published entry of `@guren/server/agent` (mirrored as
|
|
3
|
+
* `@guren/core/agent`): the half of the agent surface an *out-of-process*
|
|
4
|
+
* dispatcher needs — a browser tab running WebMCP, a CLI, a test harness —
|
|
5
|
+
* as opposed to the half that only makes sense inside a running application.
|
|
6
|
+
*
|
|
7
|
+
* Everything else under `src/agent/` stays internal and is reached through
|
|
8
|
+
* the package index: the audit events, the approval queue, the scope grammar
|
|
9
|
+
* and `deriveAgentTools` itself all import from the application (`Event`, the
|
|
10
|
+
* container, `Router`), which is exactly what a client bundle must not pull
|
|
11
|
+
* in. This module is the one place that promises otherwise, so the promise is
|
|
12
|
+
* stated here rather than left to whoever next adds a re-export:
|
|
13
|
+
*
|
|
14
|
+
* - **Pure Web API.** `Request` / `Response` / `Headers` / `URLSearchParams`
|
|
15
|
+
* and nothing else. No `node:` import, no Bun global, no DOM access — the
|
|
16
|
+
* module must be importable under SSR as well as in a browser. Its two
|
|
17
|
+
* transitive imports (`../internal/route-path`, `../internal/agent-preflight`)
|
|
18
|
+
* are string and regex constants for that reason.
|
|
19
|
+
* - **Types only from `./derive`.** `dispatch.ts` imports `DerivedAgentTool`
|
|
20
|
+
* with `import type`, so the derivation — which reaches `Router` and the
|
|
21
|
+
* authorization middleware — never enters the graph. Re-exporting a *value*
|
|
22
|
+
* from `./derive` here would undo that silently: the bundle would grow the
|
|
23
|
+
* whole route layer and nothing would fail.
|
|
24
|
+
*
|
|
25
|
+
* A consumer holding a `.guren/agents.gen.ts` manifest therefore has
|
|
26
|
+
* everything needed to turn a flat tool call into the HTTP request the route
|
|
27
|
+
* validates, and the route's response back into an MCP tool result — the one
|
|
28
|
+
* dispatch contract (RFC 0016 §3), not a second one written per surface.
|
|
29
|
+
*/
|
|
30
|
+
export { advertisesStructuredOutput, buildToolRequest, describeBuildFailure, mapToolResponse, PREFLIGHT_ARGUMENT, } from './dispatch.js';
|
|
31
|
+
export type { BuildToolRequestOptions, BuiltToolRequest, ToolCallOutcome, ToolRequestBuildFailure, } from './dispatch.js';
|
|
32
|
+
export type { AgentToolInputSource, AgentToolSchema, DerivedAgentTool, DerivedAgentToolAnnotations, DerivedAgentToolExposure, } from './derive.js';
|
|
33
|
+
export type { AgentSurface } from './events.js';
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { a as mapToolResponse, i as describeBuildFailure, n as advertisesStructuredOutput, r as buildToolRequest, t as PREFLIGHT_ARGUMENT } from "../dispatch-erOS9epZ.js";
|
|
2
|
+
export { PREFLIGHT_ARGUMENT, advertisesStructuredOutput, buildToolRequest, describeBuildFailure, mapToolResponse };
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Argument masking for the agent audit trail (RFC 0016 §5.2).
|
|
3
|
+
*
|
|
4
|
+
* Agent tool arguments are logged by default, which is only safe if the
|
|
5
|
+
* logging path masks the fields that must not be written down. Two sources,
|
|
6
|
+
* unioned: a fixed list of sensitive key *fragments* every application gets
|
|
7
|
+
* without asking, and the route's own `.agent({ redact })` metadata.
|
|
8
|
+
*
|
|
9
|
+
* The rule is deliberately blunt — a key matches when its lowercased name
|
|
10
|
+
* *contains* a fragment, and that applies to the declared `redact` entries
|
|
11
|
+
* too, not just the built-ins. So `redact: ['id']` also masks `userId`, and
|
|
12
|
+
* the built-in `session` masks `sessionCount`. Over-redaction is the safe
|
|
13
|
+
* direction for a log: a masked field that did not need masking costs a
|
|
14
|
+
* debugging round trip, an unmasked credential costs a rotation.
|
|
15
|
+
*
|
|
16
|
+
* The walk is total for the same reason the derivation is: it runs while
|
|
17
|
+
* *recording* what happened, including a denial taken before the route's own
|
|
18
|
+
* validation, so it must not be the thing that throws. A cycle and a payload
|
|
19
|
+
* nested past {@link MAX_DEPTH} both terminate with a marker.
|
|
20
|
+
*/
|
|
21
|
+
/**
|
|
22
|
+
* The replacement written in place of a masked value. A string, so a redacted
|
|
23
|
+
* record still serializes as JSON and reads the same in every sink.
|
|
24
|
+
*/
|
|
25
|
+
export declare const AGENT_REDACTED = "[REDACTED]";
|
|
26
|
+
/**
|
|
27
|
+
* Written in place of a value that closes a cycle, so a self-referencing
|
|
28
|
+
* argument object produces a finite record instead of throwing or hanging.
|
|
29
|
+
* Distinct from {@link AGENT_REDACTED} on purpose: one says "we would not show
|
|
30
|
+
* you", the other "there is nothing further here to show".
|
|
31
|
+
*/
|
|
32
|
+
export declare const AGENT_CIRCULAR = "[Circular]";
|
|
33
|
+
/**
|
|
34
|
+
* Written in place of a value nested deeper than {@link MAX_DEPTH}.
|
|
35
|
+
*/
|
|
36
|
+
export declare const AGENT_TRUNCATED = "[Truncated]";
|
|
37
|
+
/**
|
|
38
|
+
* Mask the sensitive fields of an agent tool's arguments.
|
|
39
|
+
*
|
|
40
|
+
* Returns a deep copy: the input is never mutated, and a caller may keep
|
|
41
|
+
* holding the unredacted original. The copy is deep only through the
|
|
42
|
+
* structures this walks — plain objects and arrays. Anything else (a `Date`,
|
|
43
|
+
* a `Map`, a `Set`, a class instance) is carried across as the *same
|
|
44
|
+
* reference*, because copying it would need a rule per type and would still
|
|
45
|
+
* get the exotic cases wrong; nothing below such a value is inspected, so a
|
|
46
|
+
* credential hidden inside a class instance is masked only if the key holding
|
|
47
|
+
* that instance matches. Tool arguments arrive as parsed JSON on every
|
|
48
|
+
* surface, where this case does not occur; a caller synthesizing arguments in
|
|
49
|
+
* process is the one that can hit it.
|
|
50
|
+
*
|
|
51
|
+
* @param args The invocation arguments. Walked as a record whatever its
|
|
52
|
+
* prototype, so the return is always a fresh plain object.
|
|
53
|
+
* @param redact Extra key fragments from the route's `.agent({ redact })`.
|
|
54
|
+
*/
|
|
55
|
+
export declare function redactAgentArguments(args: Record<string, unknown>, redact?: readonly string[]): Record<string, unknown>;
|