Admins can now give each EXTERNAL MCP server a free-text instruction ("how/
when to use this server's tools") that the agent receives in its SYSTEM
PROMPT next to the tool descriptions — porting the built-in SERVER_INSTRUCTIONS
idea to admin-configured servers. Trusted, admin-authored text (like a system
prompt); NON-secret, so unlike headersEnc it IS returned in views/forms.
- Migration: nullable `instructions text` on ai_mcp_servers (old rows = null =
no guidance). Table type + repo insert/update (blank/whitespace -> null via
blankToNull). DTO `@MaxLength(4000)`. Service threads it through
McpServerView/toView.
- mcp-clients: `McpServerInstruction { serverName, toolPrefix, instructions }`
threaded through the toolset/cache/lease. Guidance is built ONLY for a server
that actually connected AND contributed >=1 callable tool (the allowlist may
filter all of them out) AND has non-blank text — so a guide never appears for
tools the agent cannot call. Cached with the toolset, so an edit is picked up
next turn via the existing CRUD cache invalidation.
- System prompt: `buildMcpToolingBlock` renders an <mcp_tooling> block INSIDE
the safety sandwich (after context, before the trailing SAFETY_FRAMEWORK) so
it informs tool choice but cannot override the rules; each section is headed
by the server's `prefix_*` namespace. Empty/blank -> block omitted. The
caller (ai-chat.service) now builds the external toolset BEFORE the prompt and
passes external.instructions; client-handle lifecycle (close-once) unchanged.
- Client: instructions field in types + a Textarea (autosize, maxLength 4000)
in the MCP-server form with a namespace-prefix hint; i18n (en/ru).
Tests across every layer (prompt block placement + both SAFETY copies; view
blank->null; buildEntry includes guidance only for connected+>=1-tool+non-blank;
DTO MaxLength; repo + integration round-trip; service wiring). Delegated impl
reviewed (APPROVE); applied the import-type follow-up.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
176 lines
5.9 KiB
TypeScript
176 lines
5.9 KiB
TypeScript
import { Kysely, sql } from 'kysely';
|
|
import { randomUUID } from 'node:crypto';
|
|
import { AiMcpServerRepo } from '@docmost/db/repos/ai-chat/ai-mcp-server.repo';
|
|
import { getTestDb, destroyTestDb, createWorkspace } from './db';
|
|
|
|
/**
|
|
* AiMcpServerRepo `tool_allowlist` jsonb round-trip (PR #172 / issue #173 §3).
|
|
*
|
|
* The fix under test is a DB round-trip, so a unit test cannot observe it: the
|
|
* write must land as a real jsonb ARRAY (not a double-encoded string scalar),
|
|
* and the read must repair any legacy string-scalar rows. The read-side
|
|
* `parseToolAllowlist` MASKS a write regression (it parses the string back), so
|
|
* without this integration check, reverting `::text::jsonb` to `::jsonb` would
|
|
* keep every unit test green while silently corrupting the column again.
|
|
*/
|
|
describe('AiMcpServerRepo tool_allowlist jsonb round-trip [integration]', () => {
|
|
let db: Kysely<any>;
|
|
let repo: AiMcpServerRepo;
|
|
let ws: string;
|
|
|
|
beforeAll(async () => {
|
|
db = getTestDb();
|
|
repo = new AiMcpServerRepo(db as any);
|
|
ws = (await createWorkspace(db)).id;
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await destroyTestDb();
|
|
});
|
|
|
|
const jsonbTypeof = async (id: string): Promise<string | null> => {
|
|
const res = await sql<{ t: string | null }>`
|
|
SELECT jsonb_typeof(tool_allowlist) AS t
|
|
FROM ai_mcp_servers WHERE id = ${id}
|
|
`.execute(db);
|
|
return res.rows[0]?.t ?? null;
|
|
};
|
|
|
|
it('insert stores the allowlist as a jsonb ARRAY (not a string scalar)', async () => {
|
|
const row = await repo.insert({
|
|
workspaceId: ws,
|
|
name: `srv-${randomUUID()}`,
|
|
transport: 'http',
|
|
url: 'https://example.com/mcp',
|
|
toolAllowlist: ['search', 'crawl'],
|
|
});
|
|
|
|
// The column holds a real jsonb array — the whole point of ::text::jsonb.
|
|
expect(await jsonbTypeof(row.id)).toBe('array');
|
|
|
|
// And the read returns a genuine string[], not a JSON string.
|
|
const found = await repo.findById(row.id, ws);
|
|
expect(found?.toolAllowlist).toEqual(['search', 'crawl']);
|
|
expect(Array.isArray(found?.toolAllowlist)).toBe(true);
|
|
});
|
|
|
|
it('an empty allowlist is normalized to null (no restriction), not []', async () => {
|
|
const row = await repo.insert({
|
|
workspaceId: ws,
|
|
name: `srv-${randomUUID()}`,
|
|
transport: 'http',
|
|
url: 'https://example.com/mcp',
|
|
toolAllowlist: [],
|
|
});
|
|
// The column is SQL NULL, so jsonb_typeof returns SQL NULL (JS null).
|
|
expect(await jsonbTypeof(row.id)).toBeNull();
|
|
expect((await repo.findById(row.id, ws))?.toolAllowlist).toBeNull();
|
|
});
|
|
|
|
it('repairs a legacy double-encoded (string scalar) row on read (self-heal)', async () => {
|
|
// Seed a row whose tool_allowlist is a jsonb STRING SCALAR holding the JSON
|
|
// text — exactly what the old `::jsonb` double-encoding produced.
|
|
const id = randomUUID();
|
|
await sql`
|
|
INSERT INTO ai_mcp_servers (id, workspace_id, name, transport, url, tool_allowlist)
|
|
VALUES (
|
|
${id}, ${ws}, ${`srv-${id}`}, 'http', 'https://example.com/mcp',
|
|
to_jsonb(${'["alpha","beta"]'}::text)
|
|
)
|
|
`.execute(db);
|
|
|
|
// Sanity: the seeded column really IS the corrupt string-scalar shape.
|
|
expect(await jsonbTypeof(id)).toBe('string');
|
|
|
|
// The repo read heals it back to a real string[].
|
|
expect((await repo.findById(id, ws))?.toolAllowlist).toEqual([
|
|
'alpha',
|
|
'beta',
|
|
]);
|
|
const enabled = await repo.listEnabled(ws);
|
|
const healed = enabled.find((r) => r.id === id);
|
|
expect(healed?.toolAllowlist).toEqual(['alpha', 'beta']);
|
|
});
|
|
});
|
|
|
|
/**
|
|
* AiMcpServerRepo `instructions` text round-trip (#180). The column is plain
|
|
* text (no jsonb); blank/whitespace is normalized to null on both insert and
|
|
* update so an empty guide is never persisted.
|
|
*/
|
|
describe('AiMcpServerRepo instructions round-trip [integration]', () => {
|
|
let db: Kysely<any>;
|
|
let repo: AiMcpServerRepo;
|
|
let ws: string;
|
|
|
|
beforeAll(async () => {
|
|
db = getTestDb();
|
|
repo = new AiMcpServerRepo(db as any);
|
|
ws = (await createWorkspace(db)).id;
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await destroyTestDb();
|
|
});
|
|
|
|
it('insert stores trimmed non-blank instructions and reads them back', async () => {
|
|
const row = await repo.insert({
|
|
workspaceId: ws,
|
|
name: `srv-${randomUUID()}`,
|
|
transport: 'http',
|
|
url: 'https://example.com/mcp',
|
|
instructions: ' Use search for fresh facts. ',
|
|
});
|
|
expect((await repo.findById(row.id, ws))?.instructions).toBe(
|
|
'Use search for fresh facts.',
|
|
);
|
|
});
|
|
|
|
it('insert normalizes blank/whitespace instructions to null', async () => {
|
|
const row = await repo.insert({
|
|
workspaceId: ws,
|
|
name: `srv-${randomUUID()}`,
|
|
transport: 'http',
|
|
url: 'https://example.com/mcp',
|
|
instructions: ' ',
|
|
});
|
|
expect((await repo.findById(row.id, ws))?.instructions).toBeNull();
|
|
});
|
|
|
|
it('insert with omitted instructions stores null', async () => {
|
|
const row = await repo.insert({
|
|
workspaceId: ws,
|
|
name: `srv-${randomUUID()}`,
|
|
transport: 'http',
|
|
url: 'https://example.com/mcp',
|
|
});
|
|
expect((await repo.findById(row.id, ws))?.instructions).toBeNull();
|
|
});
|
|
|
|
it('update sets, clears (blank => null), and leaves unchanged when absent', async () => {
|
|
const row = await repo.insert({
|
|
workspaceId: ws,
|
|
name: `srv-${randomUUID()}`,
|
|
transport: 'http',
|
|
url: 'https://example.com/mcp',
|
|
instructions: 'initial guide',
|
|
});
|
|
|
|
// Set a new value.
|
|
await repo.update(row.id, ws, { instructions: 'updated guide' });
|
|
expect((await repo.findById(row.id, ws))?.instructions).toBe(
|
|
'updated guide',
|
|
);
|
|
|
|
// Absent in the patch => unchanged.
|
|
await repo.update(row.id, ws, { name: 'renamed' });
|
|
expect((await repo.findById(row.id, ws))?.instructions).toBe(
|
|
'updated guide',
|
|
);
|
|
|
|
// Blank => cleared to null.
|
|
await repo.update(row.id, ws, { instructions: ' ' });
|
|
expect((await repo.findById(row.id, ws))?.instructions).toBeNull();
|
|
});
|
|
});
|