@things-factory/ai-assistant 10.1.314 → 10.1.319
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-client/utils/entry-proposal.d.ts +4 -1
- package/dist-client/utils/entry-proposal.js +5 -2
- package/dist-client/utils/entry-workspace.js +3 -3
- package/dist-server/index.d.ts +1 -0
- package/dist-server/index.js +4 -0
- package/dist-server/service/chat-session/attachment-tools.js +2 -0
- package/dist-server/service/chat-session/chat-session.d.ts +6 -0
- package/dist-server/service/chat-session/chat-session.js +9 -1
- package/dist-server/service/data-entry/entry-apply.js +3 -4
- package/dist-server/service/data-entry/entry-target.d.ts +24 -1
- package/dist-server/service/data-entry/entry-target.js +49 -8
- package/dist-server/service/data-entry/entry-tools.d.ts +4 -3
- package/dist-server/service/data-entry/entry-tools.js +50 -11
- package/dist-server/service/data-entry/entry-update.d.ts +1 -0
- package/dist-server/service/data-entry/entry-update.js +2 -0
- package/dist-server/service/data-read/read-tools.js +11 -6
- package/dist-server/service/database-error.d.ts +5 -0
- package/dist-server/service/database-error.js +27 -0
- package/dist-server/service/index.d.ts +2 -0
- package/dist-server/service/index.js +7 -1
- package/dist-server/service/mcp/mcp-audit.d.ts +7 -0
- package/dist-server/service/mcp/mcp-audit.js +44 -0
- package/dist-server/service/mcp/mcp-call.d.ts +60 -0
- package/dist-server/service/mcp/mcp-call.js +136 -0
- package/dist-server/service/mcp/mcp-conversation.d.ts +30 -0
- package/dist-server/service/mcp/mcp-conversation.js +203 -0
- package/dist-server/service/mcp/mcp-held-proposal.d.ts +19 -0
- package/dist-server/service/mcp/mcp-held-proposal.js +134 -0
- package/dist-server/service/mcp/mcp-route.d.ts +7 -0
- package/dist-server/service/mcp/mcp-route.js +50 -0
- package/dist-server/service/mcp/mcp-scope.d.ts +59 -0
- package/dist-server/service/mcp/mcp-scope.js +169 -0
- package/dist-server/service/mcp/mcp-server.d.ts +43 -0
- package/dist-server/service/mcp/mcp-server.js +163 -0
- package/dist-server/service/mcp/mcp-settings.d.ts +33 -0
- package/dist-server/service/mcp/mcp-settings.js +25 -0
- package/dist-server/service/people/invite-tools.js +3 -0
- package/dist-server/service/request-translator.d.ts +6 -0
- package/dist-server/service/request-translator.js +14 -0
- package/dist-server/service/workspace/workspace-blocks.d.ts +2 -0
- package/dist-server/service/workspace/workspace-blocks.js +3 -1
- package/dist-server/service/workspace/workspace-tools.js +3 -1
- package/package.json +7 -7
- package/translations/en.json +10 -1
- package/translations/es.json +10 -1
- package/translations/ja.json +10 -1
- package/translations/ko.json +10 -1
- package/translations/ms.json +10 -1
- package/translations/zh.json +10 -1
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { type ToolAccessCheck, type ToolScope, type ToolSpec } from '@things-factory/ai-client-base';
|
|
2
|
+
/** Whether a tool may go out at all, before asking who is calling. */
|
|
3
|
+
export declare function offeredOutside(spec: ToolSpec): boolean;
|
|
4
|
+
/** A proposal kept for a person to press in the app - it may be handed in from outside, where a conversation keeps it. */
|
|
5
|
+
export declare function handedInOutside(spec: ToolSpec): boolean;
|
|
6
|
+
/** A write its registrar says may be done from outside - before asking whether the domain turned it on. */
|
|
7
|
+
export declare function writableOutside(spec: ToolSpec): boolean;
|
|
8
|
+
/**
|
|
9
|
+
* The writes the domain turned on for outside - each declaration asked for the domain once. One that cannot answer
|
|
10
|
+
* is off: a write is never opened by an error.
|
|
11
|
+
*/
|
|
12
|
+
export declare function writesOpenIn(domain: {
|
|
13
|
+
id: string;
|
|
14
|
+
} | undefined): Promise<ReadonlySet<string>>;
|
|
15
|
+
/** What may go out besides reads: a proposal kept for the person (`keeps`), and the writes the domain turned on. */
|
|
16
|
+
export interface McpReach {
|
|
17
|
+
keeps?: boolean;
|
|
18
|
+
writes?: ReadonlySet<string>;
|
|
19
|
+
}
|
|
20
|
+
/** Whether a tool goes out - a read, a proposal kept for the person where it can be kept, or a write the domain turned on. */
|
|
21
|
+
export declare const goesOutside: (reach: McpReach | boolean) => (spec: ToolSpec) => boolean;
|
|
22
|
+
/** The tools this person may call from outside - nothing for one who may not ask the assistant. */
|
|
23
|
+
export declare function mcpToolScope(access: ToolAccessCheck, mayAsk: boolean, reach?: McpReach | boolean): Promise<ToolScope>;
|
|
24
|
+
/** Why a call was not run - kept in the audit; the client is told only that the tool is unknown. */
|
|
25
|
+
export type McpRefusalCode = 'mcp-unknown-tool' | 'mcp-not-offered' | 'mcp-door-closed' | 'mcp-not-permitted';
|
|
26
|
+
export interface McpRefusal {
|
|
27
|
+
tool: string;
|
|
28
|
+
/** The category of a registered tool; absent for a name nobody registered. */
|
|
29
|
+
category?: string;
|
|
30
|
+
code: McpRefusalCode;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* One tool, asked again at the call: may go out, and this person passes every door it names. A refusal says why for
|
|
34
|
+
* the audit; the client gets the same answer for each, so it cannot tell a tool that is not theirs from no tool.
|
|
35
|
+
*/
|
|
36
|
+
export declare function findMcpTool(name: unknown, access: ToolAccessCheck, mayAsk: boolean, reach?: McpReach | boolean): Promise<{
|
|
37
|
+
spec: ToolSpec;
|
|
38
|
+
category: string;
|
|
39
|
+
} | {
|
|
40
|
+
refused: McpRefusal;
|
|
41
|
+
}>;
|
|
42
|
+
/** A tool as MCP lists it - as registered, without the arguments that only shape a screen. */
|
|
43
|
+
export declare function mcpToolOf(spec: ToolSpec): {
|
|
44
|
+
name: string;
|
|
45
|
+
description: string;
|
|
46
|
+
inputSchema: any;
|
|
47
|
+
};
|
|
48
|
+
/** The arguments a call is run with - those of a screen dropped, as they were from the schema. */
|
|
49
|
+
export declare function argsForOutside(spec: ToolSpec, args: unknown): Record<string, any>;
|
|
50
|
+
/** The most `instructions` says - a client puts it in front of its model, beside its own prompt. */
|
|
51
|
+
export declare const MCP_MAX_INSTRUCTIONS = 32000;
|
|
52
|
+
/**
|
|
53
|
+
* The rules of the categories that have a tool open to this person - the same place a conversation reads them, each
|
|
54
|
+
* category's lines already narrowed to the tools open here (`ScopedCategory.guidance`).
|
|
55
|
+
*
|
|
56
|
+
* A rule many categories say alike (every list read says how to count) is said once. What would pass the limit is left
|
|
57
|
+
* out whole, in registration order, and the last line says how much: a tool's own description still says what it does.
|
|
58
|
+
*/
|
|
59
|
+
export declare function mcpInstructions(scope: ToolScope): string;
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MCP_MAX_INSTRUCTIONS = exports.goesOutside = void 0;
|
|
4
|
+
exports.offeredOutside = offeredOutside;
|
|
5
|
+
exports.handedInOutside = handedInOutside;
|
|
6
|
+
exports.writableOutside = writableOutside;
|
|
7
|
+
exports.writesOpenIn = writesOpenIn;
|
|
8
|
+
exports.mcpToolScope = mcpToolScope;
|
|
9
|
+
exports.findMcpTool = findMcpTool;
|
|
10
|
+
exports.mcpToolOf = mcpToolOf;
|
|
11
|
+
exports.argsForOutside = argsForOutside;
|
|
12
|
+
exports.mcpInstructions = mcpInstructions;
|
|
13
|
+
/*
|
|
14
|
+
* Which registered tools an agent outside the application may call, for the person who connected it (ADR-0237
|
|
15
|
+
* decisions 1 · 2 · 3).
|
|
16
|
+
*
|
|
17
|
+
* The registry as it is - no tool is made for this door, and a tool's name, description and schema go out as
|
|
18
|
+
* registered. Step 1 opens reading only:
|
|
19
|
+
* - `kind: 'read'` - `write` · `action` · `external` change something, and no one here confirms it;
|
|
20
|
+
* - doors named - `doors: []` hands back what a screen sent, and there is no screen; no `doors` is closed everywhere;
|
|
21
|
+
* - not `proposes` - a card or an entry draft waits for a person to press it, and nobody is there to;
|
|
22
|
+
* - no `needs` - the outside agent has no stored conversation and no open screen to answer from;
|
|
23
|
+
* - every door passed - the same question the conversation asks (`toolAccessFor`), asked again at the call;
|
|
24
|
+
* - no `Mutation.` door - a tool that reaches a write is not a read, whatever its marks say. A mark forgotten closes
|
|
25
|
+
* the tool instead of opening it.
|
|
26
|
+
*
|
|
27
|
+
* Who registered a tool says whether it proposes or what it needs; this door reads the marks and knows no tool by name.
|
|
28
|
+
*
|
|
29
|
+
* Step 2 opens one kind of proposal (ADR-0237 decision 11): one whose card is kept with a conversation and pressed later
|
|
30
|
+
* by a person in the app (`keepsProposal` - the 입력안 of a data entry target, whatever app registered it). It names the
|
|
31
|
+
* door of the write it would do (`Mutation.…`): running it writes nothing, and the write runs only when the person presses
|
|
32
|
+
* the card, as them, asked again then. A tool with such a door and without the mark stays closed - a mark forgotten
|
|
33
|
+
* still closes it.
|
|
34
|
+
*
|
|
35
|
+
* A write goes out only where its registrar declared it may (`writesOutside`, ADR-0237 amendment 1) and that
|
|
36
|
+
* declaration answers yes for the token's domain - the registrar's own setting, asked at every list and every call.
|
|
37
|
+
* It writes as the person who connected, through its doors, each asked as for any other tool. Nothing here knows which
|
|
38
|
+
* tool, module or setting that is.
|
|
39
|
+
*/
|
|
40
|
+
const ai_client_base_1 = require("@things-factory/ai-client-base");
|
|
41
|
+
/** Whether a tool may go out at all, before asking who is calling. */
|
|
42
|
+
function offeredOutside(spec) {
|
|
43
|
+
return (spec.kind === 'read' &&
|
|
44
|
+
Array.isArray(spec.doors) &&
|
|
45
|
+
spec.doors.length > 0 &&
|
|
46
|
+
!spec.doors.some(door => door.startsWith('Mutation.')) &&
|
|
47
|
+
!spec.proposes &&
|
|
48
|
+
!spec.needs?.length);
|
|
49
|
+
}
|
|
50
|
+
/** A proposal kept for a person to press in the app - it may be handed in from outside, where a conversation keeps it. */
|
|
51
|
+
function handedInOutside(spec) {
|
|
52
|
+
return spec.kind === 'read' && Array.isArray(spec.doors) && spec.doors.length > 0 && !!spec.proposes && !!spec.keepsProposal && !spec.needs?.length;
|
|
53
|
+
}
|
|
54
|
+
/** A write its registrar says may be done from outside - before asking whether the domain turned it on. */
|
|
55
|
+
function writableOutside(spec) {
|
|
56
|
+
/* `external` runs on the server and answers; a `write` or `action` is applied by a screen, and there is none here. */
|
|
57
|
+
return spec.kind === 'external' && Array.isArray(spec.doors) && spec.doors.length > 0 && !!spec.writesOutside && !spec.proposes && !spec.needs?.length;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* The writes the domain turned on for outside - each declaration asked for the domain once. One that cannot answer
|
|
61
|
+
* is off: a write is never opened by an error.
|
|
62
|
+
*/
|
|
63
|
+
async function writesOpenIn(domain) {
|
|
64
|
+
const open = new Set();
|
|
65
|
+
if (!domain?.id)
|
|
66
|
+
return open;
|
|
67
|
+
const writes = (0, ai_client_base_1.getToolCategories)().flatMap(category => category.specs.filter(writableOutside));
|
|
68
|
+
await Promise.all(writes.map(async (spec) => {
|
|
69
|
+
if (await spec.writesOutside.enabledFor(domain).catch(() => false))
|
|
70
|
+
open.add(spec.name);
|
|
71
|
+
}));
|
|
72
|
+
return open;
|
|
73
|
+
}
|
|
74
|
+
const reachOf = (reach) => (typeof reach === 'boolean' ? { keeps: reach } : reach);
|
|
75
|
+
/** Whether a tool goes out - a read, a proposal kept for the person where it can be kept, or a write the domain turned on. */
|
|
76
|
+
const goesOutside = (reach) => {
|
|
77
|
+
const { keeps = false, writes } = reachOf(reach);
|
|
78
|
+
return (spec) => offeredOutside(spec) || (keeps && handedInOutside(spec)) || (!!writes?.has(spec.name) && writableOutside(spec));
|
|
79
|
+
};
|
|
80
|
+
exports.goesOutside = goesOutside;
|
|
81
|
+
/** The tools this person may call from outside - nothing for one who may not ask the assistant. */
|
|
82
|
+
async function mcpToolScope(access, mayAsk, reach = false) {
|
|
83
|
+
if (!mayAsk)
|
|
84
|
+
return ai_client_base_1.EMPTY_TOOL_SCOPE;
|
|
85
|
+
const goes = (0, exports.goesOutside)(reach);
|
|
86
|
+
/* Only the categories with a tool that may go out are asked about - a category of cards alone asks no door. */
|
|
87
|
+
const requested = (0, ai_client_base_1.getToolCategories)()
|
|
88
|
+
.filter(category => category.specs.some(goes))
|
|
89
|
+
.map(category => category.name);
|
|
90
|
+
return (0, ai_client_base_1.narrowToolScope)(await (0, ai_client_base_1.resolveToolScope)(requested, access), goes);
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* One tool, asked again at the call: may go out, and this person passes every door it names. A refusal says why for
|
|
94
|
+
* the audit; the client gets the same answer for each, so it cannot tell a tool that is not theirs from no tool.
|
|
95
|
+
*/
|
|
96
|
+
async function findMcpTool(name, access, mayAsk, reach = false) {
|
|
97
|
+
const tool = typeof name === 'string' ? name : String(name ?? '');
|
|
98
|
+
const spec = tool ? (0, ai_client_base_1.findToolSpec)(tool) : undefined;
|
|
99
|
+
const category = tool ? (0, ai_client_base_1.findToolCategory)(tool) : undefined;
|
|
100
|
+
if (!spec || !category)
|
|
101
|
+
return { refused: { tool, code: 'mcp-unknown-tool' } };
|
|
102
|
+
if (!(0, exports.goesOutside)(reach)(spec))
|
|
103
|
+
return { refused: { tool, category, code: 'mcp-not-offered' } };
|
|
104
|
+
if (!mayAsk)
|
|
105
|
+
return { refused: { tool, category, code: 'mcp-not-permitted' } };
|
|
106
|
+
for (const door of spec.doors)
|
|
107
|
+
if (!(await access(door).catch(() => false)))
|
|
108
|
+
return { refused: { tool, category, code: 'mcp-door-closed' } };
|
|
109
|
+
return { spec, category };
|
|
110
|
+
}
|
|
111
|
+
/** A tool as MCP lists it - as registered, without the arguments that only shape a screen. */
|
|
112
|
+
function mcpToolOf(spec) {
|
|
113
|
+
return {
|
|
114
|
+
name: spec.name,
|
|
115
|
+
description: spec.themeNote ? `${spec.description} ${spec.themeNote}` : spec.description,
|
|
116
|
+
inputSchema: withoutScreenArgs(spec)
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
function withoutScreenArgs(spec) {
|
|
120
|
+
const schema = spec.schema?.type === 'object' ? spec.schema : { type: 'object', properties: {} };
|
|
121
|
+
const hidden = spec.screenArgs ?? [];
|
|
122
|
+
if (!hidden.length)
|
|
123
|
+
return schema;
|
|
124
|
+
const properties = Object.fromEntries(Object.entries(schema.properties ?? {}).filter(([name]) => !hidden.includes(name)));
|
|
125
|
+
const required = Array.isArray(schema.required) ? schema.required.filter((name) => !hidden.includes(name)) : undefined;
|
|
126
|
+
return { ...schema, properties, ...(required ? { required } : {}) };
|
|
127
|
+
}
|
|
128
|
+
/** The arguments a call is run with - those of a screen dropped, as they were from the schema. */
|
|
129
|
+
function argsForOutside(spec, args) {
|
|
130
|
+
const given = args && typeof args === 'object' && !Array.isArray(args) ? args : {};
|
|
131
|
+
const hidden = spec.screenArgs ?? [];
|
|
132
|
+
return hidden.length ? Object.fromEntries(Object.entries(given).filter(([name]) => !hidden.includes(name))) : given;
|
|
133
|
+
}
|
|
134
|
+
/** The most `instructions` says - a client puts it in front of its model, beside its own prompt. */
|
|
135
|
+
exports.MCP_MAX_INSTRUCTIONS = 32_000;
|
|
136
|
+
/**
|
|
137
|
+
* The rules of the categories that have a tool open to this person - the same place a conversation reads them, each
|
|
138
|
+
* category's lines already narrowed to the tools open here (`ScopedCategory.guidance`).
|
|
139
|
+
*
|
|
140
|
+
* A rule many categories say alike (every list read says how to count) is said once. What would pass the limit is left
|
|
141
|
+
* out whole, in registration order, and the last line says how much: a tool's own description still says what it does.
|
|
142
|
+
*/
|
|
143
|
+
function mcpInstructions(scope) {
|
|
144
|
+
const said = new Set();
|
|
145
|
+
const blocks = [];
|
|
146
|
+
let length = 0;
|
|
147
|
+
let left = 0;
|
|
148
|
+
const room = exports.MCP_MAX_INSTRUCTIONS - 120;
|
|
149
|
+
/* A line about a conversation's files or a screen's record is not said - there is neither here. */
|
|
150
|
+
const open = new Set(scope.specs.map(spec => spec.name));
|
|
151
|
+
for (const category of scope.categories) {
|
|
152
|
+
const guidance = Array.isArray(category.lines) ? (0, ai_client_base_1.guidanceOf)(category.lines, name => open.has(name), () => false) : category.guidance;
|
|
153
|
+
const lines = guidance.split('\n').filter(line => line.trim() && !said.has(line));
|
|
154
|
+
if (!lines.length)
|
|
155
|
+
continue;
|
|
156
|
+
const block = lines.join('\n');
|
|
157
|
+
if (left || length + block.length + 2 > room) {
|
|
158
|
+
left += lines.length;
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
lines.forEach(line => said.add(line));
|
|
162
|
+
blocks.push(block);
|
|
163
|
+
length += block.length + 2;
|
|
164
|
+
}
|
|
165
|
+
if (left)
|
|
166
|
+
blocks.push(`(${left} more lines of rules are left out to keep this short; each tool's description says what it does.)`);
|
|
167
|
+
return blocks.join('\n\n');
|
|
168
|
+
}
|
|
169
|
+
//# sourceMappingURL=mcp-scope.js.map
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { ToolAccessCheck, ToolTranslate } from '@things-factory/ai-client-base';
|
|
2
|
+
import { type McpOutside } from './mcp-call.js';
|
|
3
|
+
import { type McpRefusal } from './mcp-scope.js';
|
|
4
|
+
export declare const MCP_PATH = "/mcp";
|
|
5
|
+
/** The protocol versions this server speaks, newest first. Not 2025-03-26: that version sends batches, and this takes none. */
|
|
6
|
+
export declare const MCP_PROTOCOL_VERSIONS: string[];
|
|
7
|
+
export interface McpDeps {
|
|
8
|
+
/** The doors this request's person passes - `toolAccessFor`. */
|
|
9
|
+
access: (state: any) => ToolAccessCheck;
|
|
10
|
+
/** Whether they may ask the assistant at all (`ASK_ASSISTANT_GATE`). */
|
|
11
|
+
mayAsk: (state: any) => Promise<boolean>;
|
|
12
|
+
/** A call refused before it ran - written where the calls that ran are (ADR-0237 decision 4). */
|
|
13
|
+
refused?: (state: any, refusal: McpRefusal) => Promise<void>;
|
|
14
|
+
/** Where a proposal handed in from outside is kept - the person's conversation for the connection (ADR-0237 decision 11). Without it, reads alone go out. */
|
|
15
|
+
outside?: McpOutside;
|
|
16
|
+
/** This server's origin as the request reached it. */
|
|
17
|
+
origin?: (context: any) => string;
|
|
18
|
+
}
|
|
19
|
+
export interface McpRequest {
|
|
20
|
+
state: any;
|
|
21
|
+
t: ToolTranslate;
|
|
22
|
+
origin?: string;
|
|
23
|
+
}
|
|
24
|
+
/** The answer to one JSON-RPC message; undefined for a notification or a response, which get none. */
|
|
25
|
+
export declare function answerMcp(message: any, request: McpRequest, deps: McpDeps): Promise<object | undefined>;
|
|
26
|
+
/**
|
|
27
|
+
* The koa handler behind the authorization server's middleware, which has already refused a domain that turned outside
|
|
28
|
+
* agents off (the resource's `enabledFor`). A request that reached here without a person and a domain (the middleware
|
|
29
|
+
* missing) is refused, never answered as nobody.
|
|
30
|
+
*/
|
|
31
|
+
export declare function mcpEndpoint(deps: McpDeps): (context: any) => Promise<void>;
|
|
32
|
+
/**
|
|
33
|
+
* What is asked before the token is read (MCP Streamable HTTP, security and version headers):
|
|
34
|
+
* - an `Origin` that is not this server's is a page elsewhere reaching it through the browser (DNS rebinding) - 403;
|
|
35
|
+
* - an `MCP-Protocol-Version` this server does not speak - 400.
|
|
36
|
+
* A client that is not a browser sends no `Origin`, and the version header only after `initialize`.
|
|
37
|
+
*/
|
|
38
|
+
export declare function mcpRequestGuard(selfOrigin: (context: any) => string): (context: any, next: () => Promise<any>) => Promise<void>;
|
|
39
|
+
/**
|
|
40
|
+
* A body that is not JSON, answered in JSON-RPC (-32700). The body parser runs before every route and refuses it
|
|
41
|
+
* with a bare 400; this sits in front of it for `/mcp` only.
|
|
42
|
+
*/
|
|
43
|
+
export declare function mcpParseErrors(context: any, next: () => Promise<any>): Promise<any>;
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MCP_PROTOCOL_VERSIONS = exports.MCP_PATH = void 0;
|
|
4
|
+
exports.answerMcp = answerMcp;
|
|
5
|
+
exports.mcpEndpoint = mcpEndpoint;
|
|
6
|
+
exports.mcpRequestGuard = mcpRequestGuard;
|
|
7
|
+
exports.mcpParseErrors = mcpParseErrors;
|
|
8
|
+
const env_1 = require("@things-factory/env");
|
|
9
|
+
const request_translator_js_1 = require("../request-translator.js");
|
|
10
|
+
const mcp_call_js_1 = require("./mcp-call.js");
|
|
11
|
+
const mcp_scope_js_1 = require("./mcp-scope.js");
|
|
12
|
+
exports.MCP_PATH = '/mcp';
|
|
13
|
+
/** The protocol versions this server speaks, newest first. Not 2025-03-26: that version sends batches, and this takes none. */
|
|
14
|
+
exports.MCP_PROTOCOL_VERSIONS = ['2025-11-25', '2025-06-18'];
|
|
15
|
+
const PARSE_ERROR = -32700;
|
|
16
|
+
const INVALID_REQUEST = -32600;
|
|
17
|
+
const METHOD_NOT_FOUND = -32601;
|
|
18
|
+
const INVALID_PARAMS = -32602;
|
|
19
|
+
const INTERNAL_ERROR = -32603;
|
|
20
|
+
const reply = (id, result) => ({ jsonrpc: '2.0', id, result });
|
|
21
|
+
const failure = (id, code, message, data) => ({ jsonrpc: '2.0', id: id ?? null, error: { code, message, ...(data ? { data } : {}) } });
|
|
22
|
+
/** The answer to one JSON-RPC message; undefined for a notification or a response, which get none. */
|
|
23
|
+
async function answerMcp(message, request, deps) {
|
|
24
|
+
if (!message || typeof message !== 'object' || Array.isArray(message) || message.jsonrpc !== '2.0')
|
|
25
|
+
return failure(message?.id, INVALID_REQUEST, 'Invalid Request');
|
|
26
|
+
/* A response the client sent back, or a notification - neither is answered. */
|
|
27
|
+
if (typeof message.method !== 'string')
|
|
28
|
+
return 'result' in message || 'error' in message ? undefined : failure(message.id, INVALID_REQUEST, 'Invalid Request');
|
|
29
|
+
if (message.id === undefined)
|
|
30
|
+
return undefined;
|
|
31
|
+
/* A request's id is a string or a number; `null` is how a response says it could not read one. */
|
|
32
|
+
if (typeof message.id !== 'string' && typeof message.id !== 'number')
|
|
33
|
+
return failure(null, INVALID_REQUEST, 'Invalid Request');
|
|
34
|
+
const { id, method, params } = message;
|
|
35
|
+
const { state, t } = request;
|
|
36
|
+
const access = deps.access(state);
|
|
37
|
+
/* The writes the token's domain turned on - asked at each request, so turning one off closes it at the next. */
|
|
38
|
+
const reach = async () => ({ keeps: !!deps.outside, writes: await (0, mcp_scope_js_1.writesOpenIn)(state?.domain) });
|
|
39
|
+
switch (method) {
|
|
40
|
+
case 'initialize': {
|
|
41
|
+
const asked = params?.protocolVersion;
|
|
42
|
+
const scope = await (0, mcp_scope_js_1.mcpToolScope)(access, await deps.mayAsk(state), await reach());
|
|
43
|
+
const instructions = (0, mcp_scope_js_1.mcpInstructions)(scope);
|
|
44
|
+
return reply(id, {
|
|
45
|
+
protocolVersion: exports.MCP_PROTOCOL_VERSIONS.includes(asked) ? asked : exports.MCP_PROTOCOL_VERSIONS[0],
|
|
46
|
+
capabilities: { tools: {} },
|
|
47
|
+
serverInfo: { name: 'ai-assistant', version: '1' },
|
|
48
|
+
...(instructions ? { instructions } : {})
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
case 'ping':
|
|
52
|
+
return reply(id, {});
|
|
53
|
+
case 'tools/list': {
|
|
54
|
+
const scope = await (0, mcp_scope_js_1.mcpToolScope)(access, await deps.mayAsk(state), await reach());
|
|
55
|
+
return reply(id, { tools: scope.specs.map(mcp_scope_js_1.mcpToolOf) });
|
|
56
|
+
}
|
|
57
|
+
case 'tools/call': {
|
|
58
|
+
const name = params?.name;
|
|
59
|
+
/* Asked again here - a door closed since the list closes the tool. */
|
|
60
|
+
const found = await (0, mcp_scope_js_1.findMcpTool)(name, access, await deps.mayAsk(state), await reach());
|
|
61
|
+
if ('refused' in found) {
|
|
62
|
+
await deps.refused?.(state, found.refused);
|
|
63
|
+
return failure(id, INVALID_PARAMS, `Unknown tool: ${name}`);
|
|
64
|
+
}
|
|
65
|
+
try {
|
|
66
|
+
return reply(id, await (0, mcp_call_js_1.callMcpTool)({ ...found, args: params?.arguments, state, t, access, client: state?.oauth?.clientId, outside: deps.outside, origin: request.origin }));
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
if (error instanceof mcp_call_js_1.McpKeepFailed)
|
|
70
|
+
return failure(id, INTERNAL_ERROR, error.message);
|
|
71
|
+
throw error;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
default:
|
|
75
|
+
return failure(id, METHOD_NOT_FOUND, `Method not found: ${method}`);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* The koa handler behind the authorization server's middleware, which has already refused a domain that turned outside
|
|
80
|
+
* agents off (the resource's `enabledFor`). A request that reached here without a person and a domain (the middleware
|
|
81
|
+
* missing) is refused, never answered as nobody.
|
|
82
|
+
*/
|
|
83
|
+
function mcpEndpoint(deps) {
|
|
84
|
+
return async (context) => {
|
|
85
|
+
if (context.method !== 'POST') {
|
|
86
|
+
context.status = 405;
|
|
87
|
+
context.set('Allow', 'POST');
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
const { domain, user } = context.state ?? {};
|
|
91
|
+
if (!domain || !user) {
|
|
92
|
+
context.status = 401;
|
|
93
|
+
context.body = failure(null, INVALID_REQUEST, 'No person and domain - the request was not authorized');
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
/*
|
|
97
|
+
* The tools read the request's language as a scheduled run reads its own (`state.lng`), and what is said here is in
|
|
98
|
+
* it. A request that names none - an agent sends no cookie and no `Accept-Language` - speaks the person's: their
|
|
99
|
+
* locale, else the installation's default, as a notice picks its receiver's (`deliverNotice`). Taking the request's
|
|
100
|
+
* translator as it was answered such a request in English - a Korean person's conversation 「Outside agent: …」,
|
|
101
|
+
* its card and its notice (browser check 2026-11-28).
|
|
102
|
+
*/
|
|
103
|
+
const lng = context.state.lng ?? context.lng ?? (user.locale || env_1.config.get('i18n/defaultLanguage', 'en'));
|
|
104
|
+
const state = { ...context.state, lng };
|
|
105
|
+
const answer = await answerMcp(context.request?.body, { state, t: (0, request_translator_js_1.translatorIn)(context, lng), ...(deps.origin ? { origin: deps.origin(context) } : {}) }, deps);
|
|
106
|
+
if (answer === undefined) {
|
|
107
|
+
context.status = 202;
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
context.status = 200;
|
|
111
|
+
context.body = answer;
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* What is asked before the token is read (MCP Streamable HTTP, security and version headers):
|
|
116
|
+
* - an `Origin` that is not this server's is a page elsewhere reaching it through the browser (DNS rebinding) - 403;
|
|
117
|
+
* - an `MCP-Protocol-Version` this server does not speak - 400.
|
|
118
|
+
* A client that is not a browser sends no `Origin`, and the version header only after `initialize`.
|
|
119
|
+
*/
|
|
120
|
+
function mcpRequestGuard(selfOrigin) {
|
|
121
|
+
return async (context, next) => {
|
|
122
|
+
const origin = context.get('origin');
|
|
123
|
+
if (origin) {
|
|
124
|
+
let given;
|
|
125
|
+
try {
|
|
126
|
+
given = new URL(origin).origin;
|
|
127
|
+
}
|
|
128
|
+
catch {
|
|
129
|
+
given = undefined;
|
|
130
|
+
}
|
|
131
|
+
if (!given || given === 'null' || given !== selfOrigin(context)) {
|
|
132
|
+
context.status = 403;
|
|
133
|
+
context.body = failure(null, INVALID_REQUEST, 'Origin not allowed');
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
const version = context.get('mcp-protocol-version');
|
|
138
|
+
if (version && !exports.MCP_PROTOCOL_VERSIONS.includes(version)) {
|
|
139
|
+
context.status = 400;
|
|
140
|
+
context.body = failure(null, INVALID_REQUEST, `Unsupported MCP-Protocol-Version: ${version}. This server speaks ${exports.MCP_PROTOCOL_VERSIONS.join(', ')}.`);
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
await next();
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* A body that is not JSON, answered in JSON-RPC (-32700). The body parser runs before every route and refuses it
|
|
148
|
+
* with a bare 400; this sits in front of it for `/mcp` only.
|
|
149
|
+
*/
|
|
150
|
+
async function mcpParseErrors(context, next) {
|
|
151
|
+
if (context.method !== 'POST' || context.path !== exports.MCP_PATH)
|
|
152
|
+
return next();
|
|
153
|
+
try {
|
|
154
|
+
await next();
|
|
155
|
+
}
|
|
156
|
+
catch (error) {
|
|
157
|
+
if (!(error instanceof SyntaxError) || error['status'] !== 400)
|
|
158
|
+
throw error;
|
|
159
|
+
context.status = 400;
|
|
160
|
+
context.body = failure(null, PARSE_ERROR, 'Parse error');
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
//# sourceMappingURL=mcp-server.js.map
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export declare const MCP_SETTINGS: {
|
|
2
|
+
table: {
|
|
3
|
+
outsideAgents: {
|
|
4
|
+
name: string;
|
|
5
|
+
type: "boolean";
|
|
6
|
+
level: "domain";
|
|
7
|
+
section: "connection";
|
|
8
|
+
card: string;
|
|
9
|
+
label: string;
|
|
10
|
+
hint: string;
|
|
11
|
+
order: number;
|
|
12
|
+
write: {
|
|
13
|
+
category: string;
|
|
14
|
+
privilege: "mutation";
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
text: (domain: {
|
|
19
|
+
id: string;
|
|
20
|
+
}, key: "outsideAgents", manager?: import("typeorm").EntityManager) => Promise<string>;
|
|
21
|
+
number: (domain: {
|
|
22
|
+
id: string;
|
|
23
|
+
}, key: "outsideAgents", manager?: import("typeorm").EntityManager) => Promise<number>;
|
|
24
|
+
flag: (domain: {
|
|
25
|
+
id: string;
|
|
26
|
+
}, key: "outsideAgents", manager?: import("typeorm").EntityManager) => Promise<boolean>;
|
|
27
|
+
json: <V = unknown>(domain: {
|
|
28
|
+
id: string;
|
|
29
|
+
}, key: "outsideAgents", manager?: import("typeorm").EntityManager) => Promise<V>;
|
|
30
|
+
};
|
|
31
|
+
export declare const outsideAgentsOn: (domain: {
|
|
32
|
+
id: string;
|
|
33
|
+
}) => Promise<boolean>;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.outsideAgentsOn = exports.MCP_SETTINGS = void 0;
|
|
4
|
+
const setting_base_1 = require("@things-factory/setting-base");
|
|
5
|
+
/*
|
|
6
|
+
* 「바깥 AI 연결」 (ADR-0237 decision 6) - whether an agent outside the application may connect to this domain's tools
|
|
7
|
+
* for a person who consents. Off until the domain turns it on: opening the tools to outside agents is the domain's
|
|
8
|
+
* choice, never a default. When off, the authorization server neither grants `/mcp` there nor takes a token for it.
|
|
9
|
+
*/
|
|
10
|
+
exports.MCP_SETTINGS = (0, setting_base_1.declareDomainSettings)('ai-assistant', {
|
|
11
|
+
outsideAgents: {
|
|
12
|
+
name: 'AiAssistant::outsideAgents',
|
|
13
|
+
type: 'boolean',
|
|
14
|
+
level: 'domain',
|
|
15
|
+
section: 'connection',
|
|
16
|
+
card: 'ai-assistant.setting.external-ai',
|
|
17
|
+
label: 'ai-assistant.setting.external-ai-allow',
|
|
18
|
+
hint: 'ai-assistant.setting.external-ai-hint',
|
|
19
|
+
order: 1,
|
|
20
|
+
write: { category: 'env-var', privilege: 'mutation' }
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
const outsideAgentsOn = (domain) => exports.MCP_SETTINGS.flag(domain, 'outsideAgents');
|
|
24
|
+
exports.outsideAgentsOn = outsideAgentsOn;
|
|
25
|
+
//# sourceMappingURL=mcp-settings.js.map
|
|
@@ -118,6 +118,9 @@ const spec = {
|
|
|
118
118
|
name: 'proposeInvite',
|
|
119
119
|
/* It reads people through the organization's list - the same door that list has, so it is offered where that list is. */
|
|
120
120
|
doors: ['Query.employees'],
|
|
121
|
+
/* A card the person presses, into the conversation it is asked from. */
|
|
122
|
+
proposes: true,
|
|
123
|
+
needs: ['conversation'],
|
|
121
124
|
description: 'Offer a card that brings someone into this conversation. Give the name the person used; the server finds them in ' +
|
|
122
125
|
"the organization's people list and puts them on the card (several people by that name: the card lists each, the " +
|
|
123
126
|
'person picks). The person presses the card - nobody joins until then. One call per person.',
|
|
@@ -1,2 +1,8 @@
|
|
|
1
1
|
export type Translate = (key: string, params?: Record<string, any>) => string;
|
|
2
2
|
export declare function requestTranslator(context: any): Translate;
|
|
3
|
+
/**
|
|
4
|
+
* The request's i18next fixed to one language - for a request that names no language of its own while the person it
|
|
5
|
+
* speaks for has one (`/mcp`: an agent sends no `Accept-Language`). Without a language, or without i18next on the
|
|
6
|
+
* request (koa-i18next puts it on every request of the app; a subscription has none), the request's translator.
|
|
7
|
+
*/
|
|
8
|
+
export declare function translatorIn(context: any, lng: string | undefined): Translate;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.requestTranslator = requestTranslator;
|
|
4
|
+
exports.translatorIn = translatorIn;
|
|
4
5
|
function requestTranslator(context) {
|
|
5
6
|
return (key, params) => {
|
|
6
7
|
/*
|
|
@@ -13,6 +14,19 @@ function requestTranslator(context) {
|
|
|
13
14
|
return context.t(key, params ?? {});
|
|
14
15
|
};
|
|
15
16
|
}
|
|
17
|
+
/**
|
|
18
|
+
* The request's i18next fixed to one language - for a request that names no language of its own while the person it
|
|
19
|
+
* speaks for has one (`/mcp`: an agent sends no `Accept-Language`). Without a language, or without i18next on the
|
|
20
|
+
* request (koa-i18next puts it on every request of the app; a subscription has none), the request's translator.
|
|
21
|
+
*/
|
|
22
|
+
function translatorIn(context, lng) {
|
|
23
|
+
const i18next = context?.i18next;
|
|
24
|
+
if (!lng || typeof i18next?.getFixedT !== 'function')
|
|
25
|
+
return requestTranslator(context);
|
|
26
|
+
/* A person's locale is often a region form - `ko-KR` - and the translations are kept by language. */
|
|
27
|
+
const fixed = i18next.getFixedT(String(lng).split(/[-_]/)[0].toLowerCase());
|
|
28
|
+
return (key, params) => fixed(key, params ?? {});
|
|
29
|
+
}
|
|
16
30
|
/** 프레임워크와 같은 single brace 자리표 — `{doors}`. */
|
|
17
31
|
function interpolate(text, params) {
|
|
18
32
|
return text.replace(/\{(\w+)\}/g, (whole, name) => (params && name in params ? String(params[name]) : whole));
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
/** The AI 작업 패널's tool - a rule about the panel is said where it is open. */
|
|
2
|
+
export declare const WORKSPACE_TOOL = "showInWorkspace";
|
|
1
3
|
export declare const WORKSPACE_BLOCK_KINDS: readonly ["heading", "text", "metrics", "table", "chart", "steps"];
|
|
2
4
|
export type WorkspaceBlockKind = (typeof WORKSPACE_BLOCK_KINDS)[number];
|
|
3
5
|
export declare const MAX_BLOCKS = 40;
|
|
@@ -8,10 +8,12 @@
|
|
|
8
8
|
* to be there. Pure - tested without a server.
|
|
9
9
|
*/
|
|
10
10
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
-
exports.MAX_SERIES = exports.MAX_TABLE_ROWS = exports.MAX_BLOCKS = exports.WORKSPACE_BLOCK_KINDS = void 0;
|
|
11
|
+
exports.MAX_SERIES = exports.MAX_TABLE_ROWS = exports.MAX_BLOCKS = exports.WORKSPACE_BLOCK_KINDS = exports.WORKSPACE_TOOL = void 0;
|
|
12
12
|
exports.workspaceName = workspaceName;
|
|
13
13
|
exports.normalizeWorkspace = normalizeWorkspace;
|
|
14
14
|
exports.workspaceContents = workspaceContents;
|
|
15
|
+
/** The AI 작업 패널's tool - a rule about the panel is said where it is open. */
|
|
16
|
+
exports.WORKSPACE_TOOL = 'showInWorkspace';
|
|
15
17
|
exports.WORKSPACE_BLOCK_KINDS = ['heading', 'text', 'metrics', 'table', 'chart', 'steps'];
|
|
16
18
|
exports.MAX_BLOCKS = 40;
|
|
17
19
|
exports.MAX_TABLE_ROWS = 1000;
|
|
@@ -30,8 +30,10 @@ function showInWorkspace(args) {
|
|
|
30
30
|
}
|
|
31
31
|
const spec = {
|
|
32
32
|
kind: 'read',
|
|
33
|
-
name:
|
|
33
|
+
name: workspace_blocks_js_1.WORKSPACE_TOOL,
|
|
34
34
|
doors: [],
|
|
35
|
+
/* A card the person opens beside the conversation. */
|
|
36
|
+
proposes: true,
|
|
35
37
|
description: 'Put a large result on the AI workspace - a page the person opens from a card, beside the conversation. Blocks: ' +
|
|
36
38
|
'heading{text,level}, text{markdown}, metrics{items:[{label,value,unit,note}]}, table{columns:[{name,label,type:number|date|string}],rows,caption}, ' +
|
|
37
39
|
'chart{chart:bar|line, x:[labels], series:[{name,values:[numbers]}], unit, caption}, steps{items:[{label,state:done|doing|todo|failed,note}]}.',
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@things-factory/ai-assistant",
|
|
3
|
-
"version": "10.1.
|
|
3
|
+
"version": "10.1.319",
|
|
4
4
|
"main": "dist-server/index.js",
|
|
5
5
|
"things-factory": true,
|
|
6
6
|
"author": "heartyoh",
|
|
@@ -37,14 +37,14 @@
|
|
|
37
37
|
"@operato/shell": "^10.29.69",
|
|
38
38
|
"@operato/styles": "^10.0.0",
|
|
39
39
|
"@operato/utils": "^10.29.76",
|
|
40
|
-
"@things-factory/ai-client-base": "^10.1.
|
|
40
|
+
"@things-factory/ai-client-base": "^10.1.319",
|
|
41
41
|
"@things-factory/apptool-base": "^10.1.304",
|
|
42
|
-
"@things-factory/attachment-base": "^10.1.
|
|
43
|
-
"@things-factory/auth-base": "^10.1.
|
|
42
|
+
"@things-factory/attachment-base": "^10.1.319",
|
|
43
|
+
"@things-factory/auth-base": "^10.1.319",
|
|
44
44
|
"@things-factory/env": "^10.1.298",
|
|
45
45
|
"@things-factory/export-base": "^10.1.304",
|
|
46
|
-
"@things-factory/help": "^10.1.
|
|
47
|
-
"@things-factory/setting-base": "^10.1.
|
|
46
|
+
"@things-factory/help": "^10.1.319",
|
|
47
|
+
"@things-factory/setting-base": "^10.1.319",
|
|
48
48
|
"@things-factory/shell": "^10.1.304",
|
|
49
49
|
"dompurify": "^3.0.0",
|
|
50
50
|
"exceljs": "4.2.0",
|
|
@@ -75,5 +75,5 @@
|
|
|
75
75
|
"copyfiles": "^2.4.1",
|
|
76
76
|
"rimraf": "^5.0.0"
|
|
77
77
|
},
|
|
78
|
-
"gitHead": "
|
|
78
|
+
"gitHead": "9740f0f13130271ec920b435b6d833afdf611267"
|
|
79
79
|
}
|
package/translations/en.json
CHANGED
|
@@ -564,5 +564,14 @@
|
|
|
564
564
|
"ai-assistant.usage.month": "M{month}",
|
|
565
565
|
"ai-assistant.usage.cost-note": "Costs are reference values calculated from recorded token usage and current registered prices, and may differ from actual charges. Non-token costs, such as audio transcription, are not included. Calls to models without registered prices are excluded from cost totals.",
|
|
566
566
|
"ai-assistant.agent.image-model": "Image model",
|
|
567
|
-
"ai-assistant.agent.image-model-hint": "The model pictures are made with. Empty uses {model}."
|
|
567
|
+
"ai-assistant.agent.image-model-hint": "The model pictures are made with. Empty uses {model}.",
|
|
568
|
+
"ai-assistant.setting.external-ai": "external AI connection",
|
|
569
|
+
"ai-assistant.setting.external-ai-allow": "allow AI agents outside the app to connect",
|
|
570
|
+
"ai-assistant.setting.external-ai-hint": "When on, an AI agent outside the app (such as a coding assistant) can connect for a person who consents: it reads what that person may read here, and may hand in entry drafts. A record is made only when that person presses the draft in the app.",
|
|
571
|
+
"ai-assistant.text.outside-agent-conversation": "Outside agent: {client}",
|
|
572
|
+
"ai-assistant.text.outside-agent-proposal": "{client} handed in a proposal of {count} rows. Nothing is entered until you press the card.",
|
|
573
|
+
"ai-assistant.text.outside-agent-proposal_one": "{client} handed in a proposal of {count} row. Nothing is entered until you press the card.",
|
|
574
|
+
"ai-assistant.text.outside-agent-notice-title": "A proposal waits for you",
|
|
575
|
+
"ai-assistant.text.outside-agent-notice-body": "{client} handed in a proposal. Open it and press the card to enter it.",
|
|
576
|
+
"ai-assistant.text.mcp-resource-name": "AI assistant tools"
|
|
568
577
|
}
|