Repository navigation
Expand file tree
/
Copy pathtypes.ts
More file actions
126 lines (116 loc) · 5.32 KB
/
Copy pathtypes.ts
File metadata and controls
126 lines (116 loc) · 5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import type {
AIToolDefinition,
ToolCallPart,
ToolExecutionContext,
ToolResultPart,
} from '@objectstack/spec/contracts';
/**
* Minimal ToolRegistry interface consumed by the MCP bridge.
*
* Matches the public API of `ToolRegistry` from `@objectstack/service-ai`
* without creating a hard dependency on that package.
*/
export interface ToolRegistry {
/** Return all registered tool definitions. */
getAll(): AIToolDefinition[];
/** Execute a tool call and return the result. */
execute(toolCall: ToolCallPart): Promise<ToolExecutionResult>;
}
/**
* Extended ToolResultPart with isError flag, matching service-ai's ToolExecutionResult.
*/
export interface ToolExecutionResult extends ToolResultPart {
isError?: boolean;
}
// ── Host-injected authoring registry (HTTP, `metadata:write`) ────────────────
/**
* The door every call the HTTP MCP runtime makes into a host-injected
* authoring registry is stamped with. ONE spelling, owned by the runtime: the
* host's access check and tier mapping branch on it, and it is written by the
* runtime at the call site — never copied from anything the request carried,
* so no header a client sends can claim another door.
*/
export const MCP_TOOL_DOOR = 'mcp' as const;
export type McpToolDoor = typeof MCP_TOOL_DOOR;
/**
* The principal an OAuth access token resolves to, as the HOST verified it.
* The runtime copies it into every authoring call's context verbatim and adds
* nothing from the request: the host resolved the token, so the host is the
* only honest source.
*/
export interface McpTokenPrincipal {
/** The user the access token was issued to — the human the agent acts for. */
userId: string;
/** The organization the token's consent bound, when it bound one. */
organizationId?: string;
/** The OAuth client the token was issued to, when the host knows it. */
clientId?: string;
/** The OAuth scopes the token carries, as the host verified them. */
scopes?: readonly string[];
}
/**
* The per-call context the runtime hands a host-injected authoring tool — a
* {@link ToolExecutionContext} whose identity half is filled from the token
* and which carries the door provenance the host's access check and tier
* mapping read. The three provenance members are stamped by the runtime on
* every call (`door` is always {@link MCP_TOOL_DOOR}, `principal` and
* `environmentId` are the host's own inputs to `handleHttpRequest`); none is
* ever read from a request header, so a spoofed header changes nothing here.
*/
export interface McpAuthoringToolContext extends ToolExecutionContext {
/** Always {@link MCP_TOOL_DOOR}: this call came through the MCP door. */
door: McpToolDoor;
/** The token's user, as the actor a `ToolExecutionContext` reader expects (RLS on, as that user). */
actor: { id: string };
/** The token principal: a frozen per-call copy of what the host passed, so a tool cannot taint the next call. */
principal: McpTokenPrincipal;
/** The environment the request was dispatched for, verbatim from the host. */
environmentId?: string;
}
/**
* A host-injected authoring `ToolRegistry`: the same shape as
* {@link ToolRegistry}, except that `execute` takes the per-call
* {@link McpAuthoringToolContext} beside the call — authoring tools act on an
* environment's metadata as a principal, so a call without its context has no
* identity to act as.
*/
export interface McpAuthoringToolRegistry {
/** Return the authoring tool definitions the host chose to expose over MCP. */
getAll(): AIToolDefinition[];
/** Execute one authoring tool call under the door provenance the runtime stamped. */
execute(toolCall: ToolCallPart, context: McpAuthoringToolContext): Promise<ToolExecutionResult>;
}
/**
* What a host passes to `handleHttpRequest` to bridge its authoring registry
* for one request. The registry's tools register only when the request's
* token carries `metadata:write`; `principal` and `environmentId` become the
* provenance of every call into it.
*/
export interface McpAuthoringInjection {
/**
* The authoring registry — which tools it holds is the host's business. Its
* names must not collide with the native surface or with each other: the SDK
* throws on a duplicate registration and the request fails with it.
*/
registry: McpAuthoringToolRegistry;
/**
* The token principal the host resolved for this request — from the SAME
* token whose scopes the host passes as `toolOptions.grantedScopes`, since
* that list is what admits the registry. Copied and frozen per call.
*/
principal: McpTokenPrincipal;
/** The environment the host dispatched this request for. */
environmentId?: string;
/**
* The server instructions this request's `initialize` answers with — the
* text an MCP client puts in its model's context (where the authoring
* rules live, which ones exist). Read under the SAME grant that admits
* {@link registry}: a request whose grant registers the authoring family
* is answered with this text; any other request — a `data:read` token, an
* API key, a session — gets the runtime's configured or default
* instructions and never sees it. Omitted, the authoring request gets the
* configured or default instructions too.
*/
instructions?: string;
}