@things-factory/ai-assistant 10.1.314 → 10.1.316
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 +13 -0
- package/dist-server/service/data-entry/entry-target.js +31 -0
- 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 +1 -0
- package/dist-server/service/index.js +4 -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 +134 -0
- package/dist-server/service/mcp/mcp-conversation.d.ts +30 -0
- package/dist-server/service/mcp/mcp-conversation.js +202 -0
- package/dist-server/service/mcp/mcp-route.d.ts +7 -0
- package/dist-server/service/mcp/mcp-route.js +49 -0
- package/dist-server/service/mcp/mcp-scope.d.ts +45 -0
- package/dist-server/service/mcp/mcp-scope.js +138 -0
- package/dist-server/service/mcp/mcp-server.d.ts +43 -0
- package/dist-server/service/mcp/mcp-server.js +153 -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/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 +8 -1
- package/translations/es.json +8 -1
- package/translations/ja.json +8 -1
- package/translations/ko.json +8 -1
- package/translations/ms.json +8 -1
- package/translations/zh.json +8 -1
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.McpKeepFailed = void 0;
|
|
4
|
+
exports.callMcpTool = callMcpTool;
|
|
5
|
+
/*
|
|
6
|
+
* One tool call from outside, run where every call runs (ADR-0237 decisions 3 · 4).
|
|
7
|
+
*
|
|
8
|
+
* The context is the unattended run's shape - nobody's screen, nobody's conversation: the request's state and
|
|
9
|
+
* translator, the gate the list was built with, an empty `host` and no attachments. `conversation`, `run`, `progress`
|
|
10
|
+
* and `mode` are left out; a tool that needed them is not offered here. The call goes through `runRegisteredTool`, so
|
|
11
|
+
* the application's audit sees it on the road `mcp` with the client that made it.
|
|
12
|
+
*
|
|
13
|
+
* What comes back is cut as the conversation loop cuts it: pictures leave the JSON as image parts, and what only a
|
|
14
|
+
* card needs (`forScreen`) never goes out. A list read too long for one answer offers a card in a conversation; here
|
|
15
|
+
* there is no card, so it says it is a part and asks to narrow.
|
|
16
|
+
*/
|
|
17
|
+
const ai_client_base_1 = require("@things-factory/ai-client-base");
|
|
18
|
+
const env_1 = require("@things-factory/env");
|
|
19
|
+
const database_error_js_1 = require("../database-error.js");
|
|
20
|
+
const mcp_scope_js_1 = require("./mcp-scope.js");
|
|
21
|
+
/**
|
|
22
|
+
* A proposal that could not be kept - the conversation or its message could not be written. The call is recorded as a
|
|
23
|
+
* failure and the client gets a JSON-RPC error: it is never told a card was handed in that is not there.
|
|
24
|
+
*/
|
|
25
|
+
class McpKeepFailed extends Error {
|
|
26
|
+
constructor() {
|
|
27
|
+
super('The proposal could not be kept; nothing was handed in. Try again.');
|
|
28
|
+
this.code = 'mcp-keep-failed';
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
exports.McpKeepFailed = McpKeepFailed;
|
|
32
|
+
async function callMcpTool({ spec, category, args, state, t, access, client, outside, origin }) {
|
|
33
|
+
const keeps = !!spec.keepsProposal && !!outside;
|
|
34
|
+
/* No conversation is made before there is a card to keep - a refused call leaves none behind. */
|
|
35
|
+
const context = { state, t, access, host: {}, attachments: [], outside: { ...(client ? { client } : {}) } };
|
|
36
|
+
const answer = keeps ? (returned) => handIn(spec, category, returned, outside, state, t, origin) : forOutside;
|
|
37
|
+
let result;
|
|
38
|
+
try {
|
|
39
|
+
result = await (0, ai_client_base_1.runRegisteredTool)({ spec, category, args: (0, mcp_scope_js_1.argsForOutside)(spec, args), context, path: 'mcp', ...(client ? { client } : {}), answer });
|
|
40
|
+
}
|
|
41
|
+
catch (e) {
|
|
42
|
+
if (e instanceof McpKeepFailed)
|
|
43
|
+
throw e;
|
|
44
|
+
/* The loop answers a throw to the model the same way - the agent reads why; a database's own words stay in the log. */
|
|
45
|
+
return { content: [{ type: 'text', text: JSON.stringify({ error: ai_client_base_1.TOOL_FAILED, message: (0, database_error_js_1.databaseErrorUnspoken)(e, t) ?? String(e?.message ?? e) }) }], isError: true };
|
|
46
|
+
}
|
|
47
|
+
if (keeps && result?.handedIn === true)
|
|
48
|
+
return { content: [{ type: 'text', text: JSON.stringify(result) }] };
|
|
49
|
+
const { value: given, images } = (0, ai_client_base_1.takeToolImages)(result);
|
|
50
|
+
const { value } = (0, ai_client_base_1.takeForScreen)(given);
|
|
51
|
+
const shown = value && typeof value === 'object' && value.proposed === true ? withoutCard(value) : value;
|
|
52
|
+
return {
|
|
53
|
+
content: [
|
|
54
|
+
{ type: 'text', text: JSON.stringify(shown ?? null) },
|
|
55
|
+
/* `takeToolImages` hands back the bytes it read, never a link. */
|
|
56
|
+
...images.flatMap(({ source }) => (source.kind === 'base64' ? [{ type: 'image', data: Buffer.from(source.data).toString('base64'), mimeType: source.mediaType }] : []))
|
|
57
|
+
],
|
|
58
|
+
...(shown && typeof shown === 'object' && shown.rejected === true ? { isError: true } : {})
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* What goes out of what a tool returned - recorded as it goes out (`runRegisteredTool`'s `answer`). A card nobody here
|
|
63
|
+
* can press is a refusal: the marks should have kept the tool from going out, and a card is never passed on as an
|
|
64
|
+
* answer. A list read's panel is kept - it is cut to a part of the list below.
|
|
65
|
+
*/
|
|
66
|
+
function forOutside(result) {
|
|
67
|
+
if (!result || typeof result !== 'object' || result.proposed !== true || isListPart(result))
|
|
68
|
+
return result;
|
|
69
|
+
return { rejected: true, error: 'mcp-answer-for-a-screen', message: 'This answer is a card for a person in the app to press; it cannot be given here.' };
|
|
70
|
+
}
|
|
71
|
+
/** A list read that put its long result on the AI 작업 패널 - its rows and their count are in the answer. */
|
|
72
|
+
const isListPart = (value) => Array.isArray(value.rows) && typeof value.total === 'number';
|
|
73
|
+
/**
|
|
74
|
+
* A list read's answer that offered the panel - the card's fields gone, and the panel's sentence replaced by what is
|
|
75
|
+
* true here: the rows are a part of the list.
|
|
76
|
+
*/
|
|
77
|
+
function withoutCard(value) {
|
|
78
|
+
const { proposed: _p, kind: _k, icon: _i, label: _l, message: _m, ...said } = value;
|
|
79
|
+
const shown = Array.isArray(said.rows) ? said.rows.length : 0;
|
|
80
|
+
const total = said.total ?? said.read ?? shown;
|
|
81
|
+
const read = said.read ?? shown;
|
|
82
|
+
return {
|
|
83
|
+
...said,
|
|
84
|
+
message: `Only ${shown} of ${total} rows are here. Narrow the conditions to read the rest.` +
|
|
85
|
+
` The sums and groups are of the ${read} rows read${said.complete === false ? ', not of the whole list' : ''}; every number you say comes from them.`
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* A kept proposal handed in: its card goes into the person's conversation for the connection as the conversation loop
|
|
90
|
+
* would have kept it (the tool, the values, what the card shows, a key), and the agent is told only that it was handed
|
|
91
|
+
* in, where the person finds it, how many rows are ready, and which earlier cards of the same work it replaced - never
|
|
92
|
+
* the values it sent, which now live on the card. A result that is no proposal (a refusal) goes back as it is.
|
|
93
|
+
*/
|
|
94
|
+
async function handIn(spec, category, returned, outside, state, t, origin) {
|
|
95
|
+
const { value, forScreen } = (0, ai_client_base_1.takeForScreen)((0, ai_client_base_1.takeToolImages)(returned).value);
|
|
96
|
+
if (!value || typeof value !== 'object' || value.proposed !== true)
|
|
97
|
+
return returned;
|
|
98
|
+
const candidate = { tool: spec.name, ...value, ...forScreen };
|
|
99
|
+
const effectKey = (0, ai_client_base_1.proposalEffectKey)(candidate);
|
|
100
|
+
const card = { ...candidate, key: typeof candidate.key === 'string' && candidate.key ? candidate.key : effectKey, effectKey };
|
|
101
|
+
let conversation;
|
|
102
|
+
let kept;
|
|
103
|
+
try {
|
|
104
|
+
conversation = await outside.conversationOf(state, t);
|
|
105
|
+
kept = await outside.keep({ origin, sessionId: conversation.id, tool: spec.name, category, doors: spec.doors ?? [], card, state, t });
|
|
106
|
+
}
|
|
107
|
+
catch (error) {
|
|
108
|
+
env_1.logger.error('[ai-assistant] a proposal handed in from outside could not be kept', error);
|
|
109
|
+
throw new McpKeepFailed();
|
|
110
|
+
}
|
|
111
|
+
const counts = (value.counts ?? {});
|
|
112
|
+
const rows = Array.isArray(value.rows) ? value.rows : [];
|
|
113
|
+
const blockedRows = rows
|
|
114
|
+
.filter(row => (row?.issues ?? []).some((issue) => issue?.blocking))
|
|
115
|
+
.map(row => ({ row: row.index, issues: (row.issues ?? []).filter((issue) => issue?.blocking).map((issue) => issue.code) }));
|
|
116
|
+
const replaced = kept.replaced ?? [];
|
|
117
|
+
return {
|
|
118
|
+
handedIn: true,
|
|
119
|
+
target: value.target,
|
|
120
|
+
...(value.op ? { op: value.op } : {}),
|
|
121
|
+
rows: counts.rows ?? rows.length,
|
|
122
|
+
ready: counts.ready ?? rows.length - blockedRows.length,
|
|
123
|
+
blocked: counts.blocked ?? blockedRows.length,
|
|
124
|
+
...(blockedRows.length ? { blockedRows } : {}),
|
|
125
|
+
/* Always said: the earlier cards of the same work this one replaced - they can no longer be pressed. */
|
|
126
|
+
replaced,
|
|
127
|
+
conversation: conversation.name,
|
|
128
|
+
address: kept.address,
|
|
129
|
+
message: 'The proposal is kept in the person\'s assistant conversation named above; nothing is written until they press its card in the app, where everything is asked again. ' +
|
|
130
|
+
(replaced.length ? `It replaced ${replaced.length} earlier card(s) of the same work, which can no longer be pressed. ` : '') +
|
|
131
|
+
(blockedRows.length ? 'Rows kept out are listed by their issue codes - fix them and hand the whole set in again; the newest card of the same work replaces the earlier one.' : 'Tell the person where to find it.')
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
//# sourceMappingURL=mcp-call.js.map
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { ToolTranslate } from '@things-factory/ai-client-base';
|
|
2
|
+
import type { McpKept, McpOutside } from './mcp-call.js';
|
|
3
|
+
export interface AssistantHome {
|
|
4
|
+
anchorType: string;
|
|
5
|
+
anchorId: string;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* The anchor an app's assistant opens at home - where a conversation made on the server for a person (an outside
|
|
9
|
+
* connection's) is listed. Called by the app at boot; the domain's own anchor when nobody calls it.
|
|
10
|
+
*/
|
|
11
|
+
export declare function registerAssistantHome(home: (domain: {
|
|
12
|
+
id: string;
|
|
13
|
+
}) => AssistantHome): void;
|
|
14
|
+
export declare function assistantHomeOf(domain: {
|
|
15
|
+
id: string;
|
|
16
|
+
}): AssistantHome;
|
|
17
|
+
/** The person's conversation for this connection - the same one each time; made through the conversation's own door. */
|
|
18
|
+
export declare function outsideConversationOf(state: any, t: ToolTranslate): Promise<{
|
|
19
|
+
id: string;
|
|
20
|
+
name: string;
|
|
21
|
+
}>;
|
|
22
|
+
/**
|
|
23
|
+
* The card kept as a message of that conversation, as a turn keeps it (`proposals`, the doors of the tool that made it,
|
|
24
|
+
* the person as its writer), the conversation's activity moved on, and the person told - with where to find it.
|
|
25
|
+
*/
|
|
26
|
+
export declare function keepOutsideProposal(kept: McpKept): Promise<{
|
|
27
|
+
address: string;
|
|
28
|
+
replaced: string[];
|
|
29
|
+
}>;
|
|
30
|
+
export declare const OUTSIDE_CONVERSATION: McpOutside;
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.OUTSIDE_CONVERSATION = void 0;
|
|
37
|
+
exports.registerAssistantHome = registerAssistantHome;
|
|
38
|
+
exports.assistantHomeOf = assistantHomeOf;
|
|
39
|
+
exports.outsideConversationOf = outsideConversationOf;
|
|
40
|
+
exports.keepOutsideProposal = keepOutsideProposal;
|
|
41
|
+
let homeOf = domain => ({ anchorType: 'domain', anchorId: domain.id });
|
|
42
|
+
/**
|
|
43
|
+
* The anchor an app's assistant opens at home - where a conversation made on the server for a person (an outside
|
|
44
|
+
* connection's) is listed. Called by the app at boot; the domain's own anchor when nobody calls it.
|
|
45
|
+
*/
|
|
46
|
+
function registerAssistantHome(home) {
|
|
47
|
+
homeOf = home;
|
|
48
|
+
}
|
|
49
|
+
function assistantHomeOf(domain) {
|
|
50
|
+
return homeOf(domain);
|
|
51
|
+
}
|
|
52
|
+
const say = (t, key, defaultValue, params = {}) => t(`ai-assistant.text.${key}`, { ...params, defaultValue });
|
|
53
|
+
/** The client's name as the domain registered it; its id when there is none. */
|
|
54
|
+
async function clientNameOf(state) {
|
|
55
|
+
const clientId = state?.oauth?.clientId;
|
|
56
|
+
if (!clientId)
|
|
57
|
+
return 'client';
|
|
58
|
+
try {
|
|
59
|
+
const { Application } = await Promise.resolve().then(() => __importStar(require('@things-factory/auth-base')));
|
|
60
|
+
const { getRepository } = await Promise.resolve().then(() => __importStar(require('@things-factory/shell')));
|
|
61
|
+
const found = await getRepository(Application).findOne({ where: { appKey: clientId, domain: { id: state.domain.id } } });
|
|
62
|
+
return found?.name || clientId;
|
|
63
|
+
}
|
|
64
|
+
catch {
|
|
65
|
+
return clientId;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/** The person's conversation for this connection - the same one each time; made through the conversation's own door. */
|
|
69
|
+
async function outsideConversationOf(state, t) {
|
|
70
|
+
const { domain, user, oauth } = state ?? {};
|
|
71
|
+
const connectionId = oauth?.grantId;
|
|
72
|
+
if (!domain?.id || !user?.id || !connectionId)
|
|
73
|
+
throw new Error('an outside conversation needs the person, the domain and the connection');
|
|
74
|
+
const [{ getRepository, isUniqueViolation }, { ChatSession }, { ChatSessionParticipant }, { ChatSessionResolver }] = await Promise.all([
|
|
75
|
+
Promise.resolve().then(() => __importStar(require('@things-factory/shell'))),
|
|
76
|
+
Promise.resolve().then(() => __importStar(require('../chat-session/chat-session.js'))),
|
|
77
|
+
Promise.resolve().then(() => __importStar(require('../chat-session-participant/chat-session-participant.js'))),
|
|
78
|
+
Promise.resolve().then(() => __importStar(require('../chat-session-resolver.js')))
|
|
79
|
+
]);
|
|
80
|
+
const existing = async () => (await getRepository(ChatSession).findOne({ where: { domain: { id: domain.id }, connectionId } }));
|
|
81
|
+
const found = await existing();
|
|
82
|
+
if (found)
|
|
83
|
+
return { id: found.id, name: found.name };
|
|
84
|
+
const name = say(t, 'outside-agent-conversation', 'Outside agent: {client}', { client: await clientNameOf(state) });
|
|
85
|
+
const home = assistantHomeOf(domain);
|
|
86
|
+
/* The conversation's own door - its owner seat, its visibility (private, as every new one) - then the connection. */
|
|
87
|
+
const made = await new ChatSessionResolver().createAISession(home.anchorType, home.anchorId, name, { state: { domain, user }, t });
|
|
88
|
+
try {
|
|
89
|
+
await getRepository(ChatSession).update(made.id, { connectionId });
|
|
90
|
+
return { id: made.id, name: made.name ?? name };
|
|
91
|
+
}
|
|
92
|
+
catch (error) {
|
|
93
|
+
/*
|
|
94
|
+
* Another first call made it at the same time - the connection's unique index keeps one. That one is the
|
|
95
|
+
* conversation; the one just made is still empty and nobody has seen it, so it goes, seat and all.
|
|
96
|
+
*/
|
|
97
|
+
if (!isUniqueViolation(error))
|
|
98
|
+
throw error;
|
|
99
|
+
const theirs = await existing();
|
|
100
|
+
if (!theirs)
|
|
101
|
+
throw error;
|
|
102
|
+
await getRepository(ChatSessionParticipant).delete({ session: { id: made.id } });
|
|
103
|
+
await getRepository(ChatSession).delete(made.id);
|
|
104
|
+
return { id: theirs.id, name: theirs.name };
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* The card kept as a message of that conversation, as a turn keeps it (`proposals`, the doors of the tool that made it,
|
|
109
|
+
* the person as its writer), the conversation's activity moved on, and the person told - with where to find it.
|
|
110
|
+
*/
|
|
111
|
+
async function keepOutsideProposal(kept) {
|
|
112
|
+
const { state, t, sessionId, card, doors, tool } = kept;
|
|
113
|
+
const { domain, user } = state;
|
|
114
|
+
const [{ getRepository }, { ChatMessage }, { ChatSession }, { publishChatMessage }, { publishSessionActivity }, { activityPatch }, { doorsForAnswer }] = await Promise.all([
|
|
115
|
+
Promise.resolve().then(() => __importStar(require('@things-factory/shell'))),
|
|
116
|
+
Promise.resolve().then(() => __importStar(require('../chat-message/chat-message.js'))),
|
|
117
|
+
Promise.resolve().then(() => __importStar(require('../chat-session/chat-session.js'))),
|
|
118
|
+
Promise.resolve().then(() => __importStar(require('../chat-message/chat-message-publish.js'))),
|
|
119
|
+
Promise.resolve().then(() => __importStar(require('../chat-session/session-activity-publish.js'))),
|
|
120
|
+
Promise.resolve().then(() => __importStar(require('../chat-session/session-inbox.js'))),
|
|
121
|
+
Promise.resolve().then(() => __importStar(require('../chat-message/message-doors.js')))
|
|
122
|
+
]);
|
|
123
|
+
const client = await clientNameOf(state);
|
|
124
|
+
const replaced = await replacedBy(sessionId, card);
|
|
125
|
+
const rows = Number(card.counts?.rows ?? (Array.isArray(card.rows) ? card.rows.length : 0));
|
|
126
|
+
const content = say(t, 'outside-agent-proposal', '{client} handed in a proposal of {rows} rows. Nothing is entered until you press the card.', { client, rows });
|
|
127
|
+
/* The doors of the tool that made it (ADR-0073) - a reader who cannot pass them sees that it is here, not its values. */
|
|
128
|
+
const answerDoors = doorsForAnswer([{ name: tool, outcome: 'proposed' }], () => doors);
|
|
129
|
+
const message = await getRepository(ChatMessage).save({
|
|
130
|
+
session: { id: sessionId },
|
|
131
|
+
role: 'assistant',
|
|
132
|
+
content,
|
|
133
|
+
doors: answerDoors,
|
|
134
|
+
toolUsages: JSON.stringify([{ name: tool, arguments: {}, kind: 'read', outcome: 'proposed' }]),
|
|
135
|
+
proposals: JSON.stringify([card]),
|
|
136
|
+
creator: user,
|
|
137
|
+
updater: user
|
|
138
|
+
});
|
|
139
|
+
const session = await getRepository(ChatSession).findOne({ where: { id: sessionId } });
|
|
140
|
+
publishChatMessage({ domainId: domain.id, sessionId, message });
|
|
141
|
+
publishSessionActivity({ domainId: domain.id, sessionId, kind: 'message', anchorType: session?.anchorType, anchorId: session?.anchorId, preview: content, actorId: user.id });
|
|
142
|
+
await getRepository(ChatSession).update(sessionId, { updater: user, ...activityPatch(content, new Date(), answerDoors) });
|
|
143
|
+
const path = `assistant-workspace?session=${encodeURIComponent(sessionId)}&entry=${encodeURIComponent(String(card.key))}`;
|
|
144
|
+
const address = await linkOf(kept.origin, state, path);
|
|
145
|
+
await tellPerson(state, t, client, path);
|
|
146
|
+
return { address, replaced };
|
|
147
|
+
}
|
|
148
|
+
/** The earlier cards of the conversation the new card replaces - of the same work, still the newest of it, not yet pressed. */
|
|
149
|
+
async function replacedBy(sessionId, card) {
|
|
150
|
+
const [{ getRepository }, { Like, Not, IsNull }, { ChatMessage }, { workOfProposal }, { entryProposalAnswered, findEntryProposal }, { PROPOSAL_OUTCOME_PREFIX }] = await Promise.all([
|
|
151
|
+
Promise.resolve().then(() => __importStar(require('@things-factory/shell'))),
|
|
152
|
+
Promise.resolve().then(() => __importStar(require('typeorm'))),
|
|
153
|
+
Promise.resolve().then(() => __importStar(require('../chat-message/chat-message.js'))),
|
|
154
|
+
Promise.resolve().then(() => __importStar(require('../data-entry/entry-target.js'))),
|
|
155
|
+
Promise.resolve().then(() => __importStar(require('../data-entry/entry-apply.js'))),
|
|
156
|
+
Promise.resolve().then(() => __importStar(require('../chat-message/proposal-outcome.js')))
|
|
157
|
+
]);
|
|
158
|
+
const carrying = await getRepository(ChatMessage).find({ where: { session: { id: sessionId }, proposals: Not(IsNull()) }, order: { createdAt: 'ASC' } });
|
|
159
|
+
const messages = carrying.map(message => ({ id: message.id, proposals: message.proposalsJson ?? [] }));
|
|
160
|
+
const work = workOfProposal(card);
|
|
161
|
+
const keys = messages.flatMap(message => message.proposals.filter((one) => one?.kind === 'entry' && one?.key && workOfProposal(one) === work).map((one) => one.key));
|
|
162
|
+
const open = [];
|
|
163
|
+
for (const key of keys) {
|
|
164
|
+
if (findEntryProposal(messages, key).superseded)
|
|
165
|
+
continue;
|
|
166
|
+
const lines = await getRepository(ChatMessage).find({ select: ['content'], where: { session: { id: sessionId }, role: 'system', content: Like(`${PROPOSAL_OUTCOME_PREFIX}${key}:%`) } });
|
|
167
|
+
if (!entryProposalAnswered(key, lines.map(line => line.content)))
|
|
168
|
+
open.push(key);
|
|
169
|
+
}
|
|
170
|
+
return open;
|
|
171
|
+
}
|
|
172
|
+
/** A page of the person's domain as a full address - what the agent hands its user. */
|
|
173
|
+
async function linkOf(origin, state, path) {
|
|
174
|
+
try {
|
|
175
|
+
const { getContextPath } = await Promise.resolve().then(() => __importStar(require('@things-factory/shell')));
|
|
176
|
+
return `${String(origin ?? '').replace(/\/+$/, '')}/${getContextPath(state.domain)}${path}`;
|
|
177
|
+
}
|
|
178
|
+
catch {
|
|
179
|
+
return path;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
/** The person told that something waits for them - kept in their inbox, live and by push (notification's `sendNotice`). */
|
|
183
|
+
async function tellPerson(state, t, client, address) {
|
|
184
|
+
if (typeof state?.sendNotice !== 'function')
|
|
185
|
+
return;
|
|
186
|
+
const title = say(t, 'outside-agent-notice-title', 'A proposal waits for you');
|
|
187
|
+
const body = say(t, 'outside-agent-notice-body', '{client} handed in a proposal. Open it and press the card to enter it.', { client });
|
|
188
|
+
try {
|
|
189
|
+
await state.sendNotice({
|
|
190
|
+
type: 'assistant-outside-proposal',
|
|
191
|
+
receivers: [{ id: state.user.id }],
|
|
192
|
+
words: () => ({ subject: title, summary: body, body, action: title, note: '' }),
|
|
193
|
+
address,
|
|
194
|
+
label: 'assistant-outside-proposal'
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
catch {
|
|
198
|
+
/* the card is kept; the notice only tells of it */
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
exports.OUTSIDE_CONVERSATION = { conversationOf: outsideConversationOf, keep: keepOutsideProposal };
|
|
202
|
+
//# sourceMappingURL=mcp-conversation.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { MCP_PATH } from './mcp-server.js';
|
|
2
|
+
export { MCP_PATH };
|
|
3
|
+
/**
|
|
4
|
+
* The shell calls each module's at boot, before the body parser - a body that is not JSON is answered in JSON-RPC.
|
|
5
|
+
* Once per app: board-ai re-exports this package, so the shell finds it twice.
|
|
6
|
+
*/
|
|
7
|
+
export declare function initMiddlewares(app: any): void;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MCP_PATH = void 0;
|
|
4
|
+
exports.initMiddlewares = initMiddlewares;
|
|
5
|
+
/*
|
|
6
|
+
* `/mcp` mounted (ADR-0237 decisions 5 · 9) - every application has the assistant, so every application has this door
|
|
7
|
+
* by depending on it. This module declares the resource and mounts the endpoint; the authorization server
|
|
8
|
+
* (`auth-base`) issues tokens for it, publishes its metadata, checks each token and sets the person and the domain.
|
|
9
|
+
*/
|
|
10
|
+
const auth_base_1 = require("@things-factory/auth-base");
|
|
11
|
+
const ai_client_base_1 = require("@things-factory/ai-client-base");
|
|
12
|
+
const tool_access_js_1 = require("../tool-access.js");
|
|
13
|
+
const run_as_js_1 = require("../unattended-run/run-as.js");
|
|
14
|
+
const mcp_audit_js_1 = require("./mcp-audit.js");
|
|
15
|
+
const mcp_conversation_js_1 = require("./mcp-conversation.js");
|
|
16
|
+
const mcp_server_js_1 = require("./mcp-server.js");
|
|
17
|
+
Object.defineProperty(exports, "MCP_PATH", { enumerable: true, get: function () { return mcp_server_js_1.MCP_PATH; } });
|
|
18
|
+
const mcp_settings_js_1 = require("./mcp-settings.js");
|
|
19
|
+
(0, auth_base_1.registerProtectedResource)({ path: mcp_server_js_1.MCP_PATH, name: 'AI assistant tools', scopes: [], enabledFor: mcp_settings_js_1.outsideAgentsOn });
|
|
20
|
+
/* Every call from outside, in the framework audit - the calls that ran here, the refused ones from the endpoint. */
|
|
21
|
+
(0, ai_client_base_1.registerToolCallRecorder)(mcp_audit_js_1.auditMcpCall);
|
|
22
|
+
const endpoint = (0, mcp_server_js_1.mcpEndpoint)({
|
|
23
|
+
access: state => (0, tool_access_js_1.toolAccessFor)({ state }),
|
|
24
|
+
/* The door a person passes to ask the assistant - the same one an unattended run's owner is judged by. */
|
|
25
|
+
mayAsk: state => (0, auth_base_1.checkPermission)(run_as_js_1.ASK_ASSISTANT_GATE, state.user, state.domain, state.unsafeIP, state.prohibitedPrivileges),
|
|
26
|
+
refused: mcp_audit_js_1.auditMcpRefusal,
|
|
27
|
+
/* An entry draft from outside waits in the person's conversation for the connection (ADR-0237 decision 11). */
|
|
28
|
+
outside: mcp_conversation_js_1.OUTSIDE_CONVERSATION,
|
|
29
|
+
origin: auth_base_1.issuerOf
|
|
30
|
+
});
|
|
31
|
+
/*
|
|
32
|
+
* A global public route: the session middleware does not run there, and the token check is the resource's own. What
|
|
33
|
+
* needs no token is asked first - another origin, a protocol version this does not speak.
|
|
34
|
+
*/
|
|
35
|
+
process.on('bootstrap-module-global-public-route', (_app, globalPublicRouter) => {
|
|
36
|
+
globalPublicRouter.all(mcp_server_js_1.MCP_PATH, (0, mcp_server_js_1.mcpRequestGuard)(auth_base_1.issuerOf), (0, auth_base_1.protectedResourceMiddleware)(mcp_server_js_1.MCP_PATH), endpoint);
|
|
37
|
+
});
|
|
38
|
+
const installed = new WeakSet();
|
|
39
|
+
/**
|
|
40
|
+
* The shell calls each module's at boot, before the body parser - a body that is not JSON is answered in JSON-RPC.
|
|
41
|
+
* Once per app: board-ai re-exports this package, so the shell finds it twice.
|
|
42
|
+
*/
|
|
43
|
+
function initMiddlewares(app) {
|
|
44
|
+
if (installed.has(app))
|
|
45
|
+
return;
|
|
46
|
+
installed.add(app);
|
|
47
|
+
app.use(mcp_server_js_1.mcpParseErrors);
|
|
48
|
+
}
|
|
49
|
+
//# sourceMappingURL=mcp-route.js.map
|
|
@@ -0,0 +1,45 @@
|
|
|
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
|
+
/** Whether a tool goes out - a read, or, where the door can keep a card (`keeps`), a proposal kept for the person. */
|
|
7
|
+
export declare const goesOutside: (keeps: boolean) => (spec: ToolSpec) => boolean;
|
|
8
|
+
/** The tools this person may call from outside - nothing for one who may not ask the assistant. */
|
|
9
|
+
export declare function mcpToolScope(access: ToolAccessCheck, mayAsk: boolean, keeps?: boolean): Promise<ToolScope>;
|
|
10
|
+
/** Why a call was not run - kept in the audit; the client is told only that the tool is unknown. */
|
|
11
|
+
export type McpRefusalCode = 'mcp-unknown-tool' | 'mcp-not-offered' | 'mcp-door-closed' | 'mcp-not-permitted';
|
|
12
|
+
export interface McpRefusal {
|
|
13
|
+
tool: string;
|
|
14
|
+
/** The category of a registered tool; absent for a name nobody registered. */
|
|
15
|
+
category?: string;
|
|
16
|
+
code: McpRefusalCode;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* One tool, asked again at the call: may go out, and this person passes every door it names. A refusal says why for
|
|
20
|
+
* the audit; the client gets the same answer for each, so it cannot tell a tool that is not theirs from no tool.
|
|
21
|
+
*/
|
|
22
|
+
export declare function findMcpTool(name: unknown, access: ToolAccessCheck, mayAsk: boolean, keeps?: boolean): Promise<{
|
|
23
|
+
spec: ToolSpec;
|
|
24
|
+
category: string;
|
|
25
|
+
} | {
|
|
26
|
+
refused: McpRefusal;
|
|
27
|
+
}>;
|
|
28
|
+
/** A tool as MCP lists it - as registered, without the arguments that only shape a screen. */
|
|
29
|
+
export declare function mcpToolOf(spec: ToolSpec): {
|
|
30
|
+
name: string;
|
|
31
|
+
description: string;
|
|
32
|
+
inputSchema: any;
|
|
33
|
+
};
|
|
34
|
+
/** The arguments a call is run with - those of a screen dropped, as they were from the schema. */
|
|
35
|
+
export declare function argsForOutside(spec: ToolSpec, args: unknown): Record<string, any>;
|
|
36
|
+
/** The most `instructions` says - a client puts it in front of its model, beside its own prompt. */
|
|
37
|
+
export declare const MCP_MAX_INSTRUCTIONS = 32000;
|
|
38
|
+
/**
|
|
39
|
+
* The rules of the categories that have a tool open to this person - the same place a conversation reads them, each
|
|
40
|
+
* category's lines already narrowed to the tools open here (`ScopedCategory.guidance`).
|
|
41
|
+
*
|
|
42
|
+
* A rule many categories say alike (every list read says how to count) is said once. What would pass the limit is left
|
|
43
|
+
* out whole, in registration order, and the last line says how much: a tool's own description still says what it does.
|
|
44
|
+
*/
|
|
45
|
+
export declare function mcpInstructions(scope: ToolScope): string;
|
|
@@ -0,0 +1,138 @@
|
|
|
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.mcpToolScope = mcpToolScope;
|
|
7
|
+
exports.findMcpTool = findMcpTool;
|
|
8
|
+
exports.mcpToolOf = mcpToolOf;
|
|
9
|
+
exports.argsForOutside = argsForOutside;
|
|
10
|
+
exports.mcpInstructions = mcpInstructions;
|
|
11
|
+
/*
|
|
12
|
+
* Which registered tools an agent outside the application may call, for the person who connected it (ADR-0237
|
|
13
|
+
* decisions 1 · 2 · 3).
|
|
14
|
+
*
|
|
15
|
+
* The registry as it is - no tool is made for this door, and a tool's name, description and schema go out as
|
|
16
|
+
* registered. Step 1 opens reading only:
|
|
17
|
+
* - `kind: 'read'` - `write` · `action` · `external` change something, and no one here confirms it;
|
|
18
|
+
* - doors named - `doors: []` hands back what a screen sent, and there is no screen; no `doors` is closed everywhere;
|
|
19
|
+
* - not `proposes` - a card or an entry draft waits for a person to press it, and nobody is there to;
|
|
20
|
+
* - no `needs` - the outside agent has no stored conversation and no open screen to answer from;
|
|
21
|
+
* - every door passed - the same question the conversation asks (`toolAccessFor`), asked again at the call;
|
|
22
|
+
* - no `Mutation.` door - a tool that reaches a write is not a read, whatever its marks say. A mark forgotten closes
|
|
23
|
+
* the tool instead of opening it.
|
|
24
|
+
*
|
|
25
|
+
* Who registered a tool says whether it proposes or what it needs; this door reads the marks and knows no tool by name.
|
|
26
|
+
*
|
|
27
|
+
* Step 2 opens one kind of proposal (ADR-0237 decision 11): one whose card is kept with a conversation and pressed later
|
|
28
|
+
* by a person in the app (`keepsProposal` - the 입력안 of a data entry target, whatever app registered it). It names the
|
|
29
|
+
* door of the write it would do (`Mutation.…`), and that is the one place a `Mutation.` door goes out: running it writes
|
|
30
|
+
* nothing, and the write runs only when the person presses the card, as them, asked again then. A tool with such a door
|
|
31
|
+
* and without the mark stays closed - a mark forgotten still closes it.
|
|
32
|
+
*/
|
|
33
|
+
const ai_client_base_1 = require("@things-factory/ai-client-base");
|
|
34
|
+
/** Whether a tool may go out at all, before asking who is calling. */
|
|
35
|
+
function offeredOutside(spec) {
|
|
36
|
+
return (spec.kind === 'read' &&
|
|
37
|
+
Array.isArray(spec.doors) &&
|
|
38
|
+
spec.doors.length > 0 &&
|
|
39
|
+
!spec.doors.some(door => door.startsWith('Mutation.')) &&
|
|
40
|
+
!spec.proposes &&
|
|
41
|
+
!spec.needs?.length);
|
|
42
|
+
}
|
|
43
|
+
/** A proposal kept for a person to press in the app - it may be handed in from outside, where a conversation keeps it. */
|
|
44
|
+
function handedInOutside(spec) {
|
|
45
|
+
return spec.kind === 'read' && Array.isArray(spec.doors) && spec.doors.length > 0 && !!spec.proposes && !!spec.keepsProposal && !spec.needs?.length;
|
|
46
|
+
}
|
|
47
|
+
/** Whether a tool goes out - a read, or, where the door can keep a card (`keeps`), a proposal kept for the person. */
|
|
48
|
+
const goesOutside = (keeps) => (spec) => offeredOutside(spec) || (keeps && handedInOutside(spec));
|
|
49
|
+
exports.goesOutside = goesOutside;
|
|
50
|
+
/** The tools this person may call from outside - nothing for one who may not ask the assistant. */
|
|
51
|
+
async function mcpToolScope(access, mayAsk, keeps = false) {
|
|
52
|
+
if (!mayAsk)
|
|
53
|
+
return ai_client_base_1.EMPTY_TOOL_SCOPE;
|
|
54
|
+
const goes = (0, exports.goesOutside)(keeps);
|
|
55
|
+
/* Only the categories with a tool that may go out are asked about - a category of cards alone asks no door. */
|
|
56
|
+
const requested = (0, ai_client_base_1.getToolCategories)()
|
|
57
|
+
.filter(category => category.specs.some(goes))
|
|
58
|
+
.map(category => category.name);
|
|
59
|
+
return (0, ai_client_base_1.narrowToolScope)(await (0, ai_client_base_1.resolveToolScope)(requested, access), goes);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* One tool, asked again at the call: may go out, and this person passes every door it names. A refusal says why for
|
|
63
|
+
* the audit; the client gets the same answer for each, so it cannot tell a tool that is not theirs from no tool.
|
|
64
|
+
*/
|
|
65
|
+
async function findMcpTool(name, access, mayAsk, keeps = false) {
|
|
66
|
+
const tool = typeof name === 'string' ? name : String(name ?? '');
|
|
67
|
+
const spec = tool ? (0, ai_client_base_1.findToolSpec)(tool) : undefined;
|
|
68
|
+
const category = tool ? (0, ai_client_base_1.findToolCategory)(tool) : undefined;
|
|
69
|
+
if (!spec || !category)
|
|
70
|
+
return { refused: { tool, code: 'mcp-unknown-tool' } };
|
|
71
|
+
if (!(0, exports.goesOutside)(keeps)(spec))
|
|
72
|
+
return { refused: { tool, category, code: 'mcp-not-offered' } };
|
|
73
|
+
if (!mayAsk)
|
|
74
|
+
return { refused: { tool, category, code: 'mcp-not-permitted' } };
|
|
75
|
+
for (const door of spec.doors)
|
|
76
|
+
if (!(await access(door).catch(() => false)))
|
|
77
|
+
return { refused: { tool, category, code: 'mcp-door-closed' } };
|
|
78
|
+
return { spec, category };
|
|
79
|
+
}
|
|
80
|
+
/** A tool as MCP lists it - as registered, without the arguments that only shape a screen. */
|
|
81
|
+
function mcpToolOf(spec) {
|
|
82
|
+
return {
|
|
83
|
+
name: spec.name,
|
|
84
|
+
description: spec.themeNote ? `${spec.description} ${spec.themeNote}` : spec.description,
|
|
85
|
+
inputSchema: withoutScreenArgs(spec)
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
function withoutScreenArgs(spec) {
|
|
89
|
+
const schema = spec.schema?.type === 'object' ? spec.schema : { type: 'object', properties: {} };
|
|
90
|
+
const hidden = spec.screenArgs ?? [];
|
|
91
|
+
if (!hidden.length)
|
|
92
|
+
return schema;
|
|
93
|
+
const properties = Object.fromEntries(Object.entries(schema.properties ?? {}).filter(([name]) => !hidden.includes(name)));
|
|
94
|
+
const required = Array.isArray(schema.required) ? schema.required.filter((name) => !hidden.includes(name)) : undefined;
|
|
95
|
+
return { ...schema, properties, ...(required ? { required } : {}) };
|
|
96
|
+
}
|
|
97
|
+
/** The arguments a call is run with - those of a screen dropped, as they were from the schema. */
|
|
98
|
+
function argsForOutside(spec, args) {
|
|
99
|
+
const given = args && typeof args === 'object' && !Array.isArray(args) ? args : {};
|
|
100
|
+
const hidden = spec.screenArgs ?? [];
|
|
101
|
+
return hidden.length ? Object.fromEntries(Object.entries(given).filter(([name]) => !hidden.includes(name))) : given;
|
|
102
|
+
}
|
|
103
|
+
/** The most `instructions` says - a client puts it in front of its model, beside its own prompt. */
|
|
104
|
+
exports.MCP_MAX_INSTRUCTIONS = 32_000;
|
|
105
|
+
/**
|
|
106
|
+
* The rules of the categories that have a tool open to this person - the same place a conversation reads them, each
|
|
107
|
+
* category's lines already narrowed to the tools open here (`ScopedCategory.guidance`).
|
|
108
|
+
*
|
|
109
|
+
* A rule many categories say alike (every list read says how to count) is said once. What would pass the limit is left
|
|
110
|
+
* out whole, in registration order, and the last line says how much: a tool's own description still says what it does.
|
|
111
|
+
*/
|
|
112
|
+
function mcpInstructions(scope) {
|
|
113
|
+
const said = new Set();
|
|
114
|
+
const blocks = [];
|
|
115
|
+
let length = 0;
|
|
116
|
+
let left = 0;
|
|
117
|
+
const room = exports.MCP_MAX_INSTRUCTIONS - 120;
|
|
118
|
+
/* A line about a conversation's files or a screen's record is not said - there is neither here. */
|
|
119
|
+
const open = new Set(scope.specs.map(spec => spec.name));
|
|
120
|
+
for (const category of scope.categories) {
|
|
121
|
+
const guidance = Array.isArray(category.lines) ? (0, ai_client_base_1.guidanceOf)(category.lines, name => open.has(name), () => false) : category.guidance;
|
|
122
|
+
const lines = guidance.split('\n').filter(line => line.trim() && !said.has(line));
|
|
123
|
+
if (!lines.length)
|
|
124
|
+
continue;
|
|
125
|
+
const block = lines.join('\n');
|
|
126
|
+
if (left || length + block.length + 2 > room) {
|
|
127
|
+
left += lines.length;
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
lines.forEach(line => said.add(line));
|
|
131
|
+
blocks.push(block);
|
|
132
|
+
length += block.length + 2;
|
|
133
|
+
}
|
|
134
|
+
if (left)
|
|
135
|
+
blocks.push(`(${left} more lines of rules are left out to keep this short; each tool's description says what it does.)`);
|
|
136
|
+
return blocks.join('\n\n');
|
|
137
|
+
}
|
|
138
|
+
//# 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>;
|