@unson/brainbase-mcp 0.51.8 → 0.51.10
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.
|
@@ -28,6 +28,7 @@ import { accessSync, closeSync, constants as fsConstants, fstatSync, lstatSync,
|
|
|
28
28
|
import { homedir } from 'node:os';
|
|
29
29
|
import { basename, delimiter, dirname, isAbsolute, join, relative, resolve, sep } from 'node:path';
|
|
30
30
|
import { fileURLToPath } from 'node:url';
|
|
31
|
+
import { types } from 'node:util';
|
|
31
32
|
import { BrainbaseLoginError, getAccessTokenOfLogin } from './brainbase-login.js';
|
|
32
33
|
import { PRE_TOOL_USE_TOOLS, REGISTRATION_ID, bearerTransportSafe, canonicalFilePath, canonicalRepositoryIdentifier, digestSummary, forwardedStopPayload, hostCanMapRepository, resolveRepositoryScope, shellQuote, singleQuotedWords, } from './claude-code-judgment-hook.js';
|
|
33
34
|
export const ANTIGRAVITY_HOOK_CONFIG_SCHEMA = 'brainbase-antigravity-judgment-hook-v1';
|
|
@@ -50,6 +51,10 @@ const DEFAULT_STALE_TURN_MS = 30 * 60 * 1000;
|
|
|
50
51
|
const MAX_CONFIG_TIMEOUT_MS = 60_000;
|
|
51
52
|
const TURN_STATE_LOCK_TIMEOUT_MS = 2_000;
|
|
52
53
|
const FLOCK_CONFLICT_EXIT_CODE = 75;
|
|
54
|
+
const START_ERRORS = [
|
|
55
|
+
'timeout', 'http_5xx', 'login_missing', 'login_expired', 'login_origin_mismatch',
|
|
56
|
+
'login_unavailable', 'transport', 'unknown',
|
|
57
|
+
];
|
|
53
58
|
function sha256(text) {
|
|
54
59
|
return createHash('sha256').update(text).digest('hex');
|
|
55
60
|
}
|
|
@@ -2343,6 +2348,9 @@ function readState(env, key) {
|
|
|
2343
2348
|
lastClosedUserStepIndex = maxStepIndex(lastClosedUserStepIndex, currentTurnUserStepIndex);
|
|
2344
2349
|
currentTurnUserStepIndex = null;
|
|
2345
2350
|
}
|
|
2351
|
+
const lastStartError = parsed.start_status === 'failed'
|
|
2352
|
+
? START_ERRORS.find((code) => code === parsed.last_start_error)
|
|
2353
|
+
: undefined;
|
|
2346
2354
|
return {
|
|
2347
2355
|
schema_version: TURN_SCHEMA,
|
|
2348
2356
|
turn_id: parsed.turn_id,
|
|
@@ -2355,6 +2363,7 @@ function readState(env, key) {
|
|
|
2355
2363
|
last_prompt_digest: typeof parsed.last_prompt_digest === 'string' ? parsed.last_prompt_digest : null,
|
|
2356
2364
|
start_status: parsed.start_status === 'started' || parsed.start_status === 'out_of_scope'
|
|
2357
2365
|
? parsed.start_status : 'failed',
|
|
2366
|
+
...(lastStartError && { last_start_error: lastStartError }),
|
|
2358
2367
|
in_git: parsed.in_git === true,
|
|
2359
2368
|
repository: typeof parsed.repository === 'string' ? parsed.repository : null,
|
|
2360
2369
|
checkout: typeof parsed.checkout === 'string' ? parsed.checkout : null,
|
|
@@ -2399,6 +2408,39 @@ function timeoutForEvent(config, antigravityEvent) {
|
|
|
2399
2408
|
function turnLogin(state) {
|
|
2400
2409
|
return { expected: typeof state.login === 'string' && state.login ? state.login : '\u0000unrecorded' };
|
|
2401
2410
|
}
|
|
2411
|
+
// Only errors created at the forwarding boundaries carry a diagnostic. Looking
|
|
2412
|
+
// up their identity avoids reading properties from an arbitrary thrown value.
|
|
2413
|
+
const forwardErrors = new WeakMap();
|
|
2414
|
+
function forwardError(code) {
|
|
2415
|
+
const error = new Error(code);
|
|
2416
|
+
forwardErrors.set(error, code);
|
|
2417
|
+
return error;
|
|
2418
|
+
}
|
|
2419
|
+
function loginStartError(error) {
|
|
2420
|
+
if (!types.isNativeError(error) || !(error instanceof BrainbaseLoginError))
|
|
2421
|
+
return 'unknown';
|
|
2422
|
+
switch (Object.getOwnPropertyDescriptor(error, 'code')?.value) {
|
|
2423
|
+
case 'login_missing': return 'login_missing';
|
|
2424
|
+
case 'login_expired': return 'login_expired';
|
|
2425
|
+
case 'login_origin_mismatch': return 'login_origin_mismatch';
|
|
2426
|
+
case 'login_busy':
|
|
2427
|
+
case 'login_changed':
|
|
2428
|
+
case 'login_credentials_invalid':
|
|
2429
|
+
case 'login_credentials_path_invalid':
|
|
2430
|
+
case 'login_credentials_path_symlink':
|
|
2431
|
+
case 'login_credentials_unreadable':
|
|
2432
|
+
case 'login_credentials_unrecognized':
|
|
2433
|
+
case 'login_deadline':
|
|
2434
|
+
case 'login_lock_lost':
|
|
2435
|
+
case 'login_refresh_unavailable':
|
|
2436
|
+
case 'login_refresh_unreachable':
|
|
2437
|
+
case 'login_storage_unsupported':
|
|
2438
|
+
case 'login_token_expiry_missing':
|
|
2439
|
+
return 'login_unavailable';
|
|
2440
|
+
default:
|
|
2441
|
+
return 'unknown';
|
|
2442
|
+
}
|
|
2443
|
+
}
|
|
2402
2444
|
async function forward(payload, context, repository, login, antigravityEvent) {
|
|
2403
2445
|
if (!hostCanMapRepository(repository, context.config))
|
|
2404
2446
|
throw new Error('judgment_repository_unmappable');
|
|
@@ -2412,12 +2454,10 @@ async function forward(payload, context, repository, login, antigravityEvent) {
|
|
|
2412
2454
|
loginId = identity.loginId;
|
|
2413
2455
|
}
|
|
2414
2456
|
catch (error) {
|
|
2415
|
-
|
|
2416
|
-
throw new Error(typeof code === 'string' && /^login_[a-z_]{1,60}$/u.test(code)
|
|
2417
|
-
? `judgment_hook_${code}` : 'judgment_hook_login_unavailable');
|
|
2457
|
+
throw forwardError(loginStartError(error));
|
|
2418
2458
|
}
|
|
2419
2459
|
if (!token || loginId === null)
|
|
2420
|
-
throw
|
|
2460
|
+
throw forwardError('login_unavailable');
|
|
2421
2461
|
if (login.expected !== undefined && login.expected !== loginId)
|
|
2422
2462
|
throw new Error('judgment_turn_login_changed');
|
|
2423
2463
|
login.used = loginId;
|
|
@@ -2426,7 +2466,10 @@ async function forward(payload, context, repository, login, antigravityEvent) {
|
|
|
2426
2466
|
session_id: antigravityNamespacedId(String(payload.session_id), context.config),
|
|
2427
2467
|
turn_id: antigravityNamespacedId(String(payload.turn_id), context.config),
|
|
2428
2468
|
};
|
|
2469
|
+
const body = JSON.stringify(sent);
|
|
2470
|
+
const signal = AbortSignal.timeout(timeoutForEvent(context.config, antigravityEvent));
|
|
2429
2471
|
let response;
|
|
2472
|
+
let text;
|
|
2430
2473
|
try {
|
|
2431
2474
|
response = await context.fetch(endpoint, {
|
|
2432
2475
|
method: 'POST',
|
|
@@ -2437,16 +2480,19 @@ async function forward(payload, context, repository, login, antigravityEvent) {
|
|
|
2437
2480
|
...(context.config.project_code ? { 'x-brainbase-project-code': context.config.project_code } : {}),
|
|
2438
2481
|
...(repository ? { 'x-brainbase-repository': repository } : {}),
|
|
2439
2482
|
},
|
|
2440
|
-
body
|
|
2483
|
+
body,
|
|
2441
2484
|
redirect: 'error',
|
|
2442
|
-
signal
|
|
2485
|
+
signal,
|
|
2443
2486
|
});
|
|
2487
|
+
text = await response.text();
|
|
2444
2488
|
}
|
|
2445
2489
|
catch (error) {
|
|
2446
|
-
|
|
2447
|
-
|
|
2490
|
+
if (signal.aborted && error === signal.reason)
|
|
2491
|
+
throw forwardError('timeout');
|
|
2492
|
+
if (types.isNativeError(error) && error instanceof TypeError)
|
|
2493
|
+
throw forwardError('transport');
|
|
2494
|
+
throw forwardError('unknown');
|
|
2448
2495
|
}
|
|
2449
|
-
const text = await response.text();
|
|
2450
2496
|
let envelope;
|
|
2451
2497
|
try {
|
|
2452
2498
|
envelope = JSON.parse(text);
|
|
@@ -2455,9 +2501,9 @@ async function forward(payload, context, repository, login, antigravityEvent) {
|
|
|
2455
2501
|
envelope = null;
|
|
2456
2502
|
}
|
|
2457
2503
|
if (!response.ok) {
|
|
2458
|
-
const
|
|
2459
|
-
|
|
2460
|
-
|
|
2504
|
+
const status = response.status;
|
|
2505
|
+
throw forwardError(Number.isInteger(status) && status >= 500 && status <= 599
|
|
2506
|
+
? 'http_5xx' : 'unknown');
|
|
2461
2507
|
}
|
|
2462
2508
|
const output = envelope?.output;
|
|
2463
2509
|
if (!envelope || envelope.schema_version !== '1' || envelope.accepted !== true
|
|
@@ -2781,6 +2827,7 @@ async function handlePreInvocation(payload, conversationId, context) {
|
|
|
2781
2827
|
writeState(context.env, key, {
|
|
2782
2828
|
...latest,
|
|
2783
2829
|
start_status: 'started',
|
|
2830
|
+
last_start_error: undefined,
|
|
2784
2831
|
login: loginId,
|
|
2785
2832
|
effective_project_code: effectiveProjectCode,
|
|
2786
2833
|
auth_fingerprint: authFingerprint,
|
|
@@ -2800,9 +2847,23 @@ async function handlePreInvocation(payload, conversationId, context) {
|
|
|
2800
2847
|
const step = buildSystemMessageStep(openedState.turn_id, additionalContext, systemMessage);
|
|
2801
2848
|
return step ? preInvocationOutputFromSteps([step]) : {};
|
|
2802
2849
|
}
|
|
2803
|
-
catch {
|
|
2804
|
-
//
|
|
2805
|
-
//
|
|
2850
|
+
catch (error) {
|
|
2851
|
+
// Keep the failed turn active so duplicate invocations do not retry.
|
|
2852
|
+
// Record the diagnostic only if the lock is immediately available.
|
|
2853
|
+
try {
|
|
2854
|
+
await withTurnStateLock(context.env, key, () => {
|
|
2855
|
+
const latest = readState(context.env, key);
|
|
2856
|
+
if (latest?.turn_active && latest.start_status === 'failed'
|
|
2857
|
+
&& latest.turn_id === openedState.turn_id
|
|
2858
|
+
&& latest.turn_generation === openedState.turn_generation) {
|
|
2859
|
+
writeState(context.env, key, {
|
|
2860
|
+
...latest,
|
|
2861
|
+
last_start_error: forwardErrors.get(error) ?? 'unknown',
|
|
2862
|
+
});
|
|
2863
|
+
}
|
|
2864
|
+
}, { ...context.turnLock, timeoutMs: 0 });
|
|
2865
|
+
}
|
|
2866
|
+
catch { }
|
|
2806
2867
|
return {};
|
|
2807
2868
|
}
|
|
2808
2869
|
}
|
|
@@ -3043,7 +3104,7 @@ export async function handleAntigravityJudgmentHook(payload, options = {}) {
|
|
|
3043
3104
|
accessToken: options.accessToken ?? (async () => {
|
|
3044
3105
|
const login = await getAccessTokenOfLogin({ fetch: options.fetch, ...options.credentials });
|
|
3045
3106
|
if (login.serverUrl !== config.login_server) {
|
|
3046
|
-
throw
|
|
3107
|
+
throw new BrainbaseLoginError('login_origin_mismatch');
|
|
3047
3108
|
}
|
|
3048
3109
|
return { token: login.token, loginId: login.loginId };
|
|
3049
3110
|
}),
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type { Tool } from '@modelcontextprotocol/sdk/types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Canonical Task comment MCP contract.
|
|
4
|
+
*
|
|
5
|
+
* The host owns authentication, tenant binding and the HTTP client. This
|
|
6
|
+
* module owns the tool schema, the request it maps to, and the check of the
|
|
7
|
+
* answer, so hosts handle comment requests consistently.
|
|
8
|
+
*/
|
|
9
|
+
export declare const ADD_TASK_COMMENT_TOOL_NAME = "add_task_comment";
|
|
10
|
+
export declare const LIST_TASK_COMMENTS_TOOL_NAME = "list_task_comments";
|
|
11
|
+
export declare const LIST_TASK_COMMENTS_DEFAULT_LIMIT = 100;
|
|
12
|
+
export declare const LIST_TASK_COMMENTS_MAX_LIMIT = 200;
|
|
13
|
+
export declare const listTaskCommentsTool: Tool;
|
|
14
|
+
export declare const addTaskCommentTool: Tool;
|
|
15
|
+
export type TaskComment = {
|
|
16
|
+
id: string;
|
|
17
|
+
body: string;
|
|
18
|
+
reply_to: string | null;
|
|
19
|
+
author: string | null;
|
|
20
|
+
created_at: string | null;
|
|
21
|
+
edited_at: string | null;
|
|
22
|
+
version: number;
|
|
23
|
+
};
|
|
24
|
+
export type TaskCommentPage = {
|
|
25
|
+
task_id: string;
|
|
26
|
+
comments: TaskComment[];
|
|
27
|
+
total_count: number;
|
|
28
|
+
truncated: boolean;
|
|
29
|
+
};
|
|
30
|
+
export type TaskCommentToolError = {
|
|
31
|
+
code: string;
|
|
32
|
+
message: string;
|
|
33
|
+
http_status?: number;
|
|
34
|
+
};
|
|
35
|
+
export type ListTaskCommentsResult = {
|
|
36
|
+
status: 'ok';
|
|
37
|
+
page: TaskCommentPage;
|
|
38
|
+
} | {
|
|
39
|
+
status: 'error';
|
|
40
|
+
error: TaskCommentToolError;
|
|
41
|
+
};
|
|
42
|
+
export declare class TaskCommentArgumentError extends Error {
|
|
43
|
+
readonly code = "invalid_arguments";
|
|
44
|
+
}
|
|
45
|
+
export declare function addTaskCommentRequest(args: unknown): {
|
|
46
|
+
method: 'POST';
|
|
47
|
+
path: string;
|
|
48
|
+
taskId: string;
|
|
49
|
+
body: {
|
|
50
|
+
body: string;
|
|
51
|
+
idempotency_key: string;
|
|
52
|
+
reply_to?: string;
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
/** The HTTP request a host sends for `list_task_comments`. Throws TaskCommentArgumentError on bad input. */
|
|
56
|
+
export declare function listTaskCommentsRequest(args: unknown): {
|
|
57
|
+
method: 'GET';
|
|
58
|
+
path: string;
|
|
59
|
+
taskId: string;
|
|
60
|
+
};
|
|
61
|
+
/**
|
|
62
|
+
* Checks the API answer for the requested Task. An error answer keeps only its
|
|
63
|
+
* code; any other body detail is dropped so a store message cannot reach the agent.
|
|
64
|
+
*/
|
|
65
|
+
export declare function readListTaskCommentsResponse(taskId: string, httpStatus: number, body: unknown): ListTaskCommentsResult;
|
|
66
|
+
export type AddTaskCommentResult = {
|
|
67
|
+
status: 'ok';
|
|
68
|
+
comment: TaskComment;
|
|
69
|
+
replayed: boolean;
|
|
70
|
+
notification: {
|
|
71
|
+
status: 'not_delivered';
|
|
72
|
+
reason: 'notification_delivery_not_configured';
|
|
73
|
+
};
|
|
74
|
+
} | {
|
|
75
|
+
status: 'error';
|
|
76
|
+
error: TaskCommentToolError;
|
|
77
|
+
};
|
|
78
|
+
export declare function readAddTaskCommentResponse(httpStatus: number, body: unknown): AddTaskCommentResult;
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Canonical Task comment MCP contract.
|
|
3
|
+
*
|
|
4
|
+
* The host owns authentication, tenant binding and the HTTP client. This
|
|
5
|
+
* module owns the tool schema, the request it maps to, and the check of the
|
|
6
|
+
* answer, so hosts handle comment requests consistently.
|
|
7
|
+
*/
|
|
8
|
+
export const ADD_TASK_COMMENT_TOOL_NAME = 'add_task_comment';
|
|
9
|
+
export const LIST_TASK_COMMENTS_TOOL_NAME = 'list_task_comments';
|
|
10
|
+
export const LIST_TASK_COMMENTS_DEFAULT_LIMIT = 100;
|
|
11
|
+
export const LIST_TASK_COMMENTS_MAX_LIMIT = 200;
|
|
12
|
+
const TASK_ID_MAX_LENGTH = 200;
|
|
13
|
+
const MAX_COMMENT_BODY_LENGTH = 20000;
|
|
14
|
+
const BODY_CONTROL = /[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f]/u;
|
|
15
|
+
const CONTROL = /[\u0000-\u001f\u007f]/u;
|
|
16
|
+
export const listTaskCommentsTool = {
|
|
17
|
+
name: LIST_TASK_COMMENTS_TOOL_NAME,
|
|
18
|
+
description: 'Read the comments on one Canonical Task, oldest first, with edits applied. '
|
|
19
|
+
+ 'Returns only what the caller\'s verified project scope can see; a Task outside it is task_not_found. '
|
|
20
|
+
+ 'This read never writes and does not notify anyone.',
|
|
21
|
+
inputSchema: {
|
|
22
|
+
type: 'object',
|
|
23
|
+
properties: {
|
|
24
|
+
task_id: { type: 'string', minLength: 1, maxLength: TASK_ID_MAX_LENGTH, description: 'Canonical Task ID (ct1....).' },
|
|
25
|
+
limit: {
|
|
26
|
+
type: 'integer',
|
|
27
|
+
minimum: 1,
|
|
28
|
+
maximum: LIST_TASK_COMMENTS_MAX_LIMIT,
|
|
29
|
+
description: `Newest comments to return, at most ${LIST_TASK_COMMENTS_MAX_LIMIT} (default ${LIST_TASK_COMMENTS_DEFAULT_LIMIT}).`,
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
required: ['task_id'],
|
|
33
|
+
additionalProperties: false,
|
|
34
|
+
},
|
|
35
|
+
annotations: { readOnlyHint: true, destructiveHint: false, idempotentHint: true, openWorldHint: false },
|
|
36
|
+
};
|
|
37
|
+
export const addTaskCommentTool = {
|
|
38
|
+
name: ADD_TASK_COMMENT_TOOL_NAME,
|
|
39
|
+
description: 'Post a comment on a Canonical Task. Reuse the same idempotency key when retrying.',
|
|
40
|
+
inputSchema: {
|
|
41
|
+
type: 'object',
|
|
42
|
+
properties: {
|
|
43
|
+
task_id: { type: 'string', minLength: 1, maxLength: TASK_ID_MAX_LENGTH },
|
|
44
|
+
body: { type: 'string', minLength: 1, maxLength: MAX_COMMENT_BODY_LENGTH },
|
|
45
|
+
reply_to: { type: 'string', minLength: 1, maxLength: TASK_ID_MAX_LENGTH },
|
|
46
|
+
idempotency_key: { type: 'string', minLength: 1, maxLength: TASK_ID_MAX_LENGTH },
|
|
47
|
+
},
|
|
48
|
+
required: ['task_id', 'body', 'idempotency_key'],
|
|
49
|
+
additionalProperties: false,
|
|
50
|
+
},
|
|
51
|
+
annotations: { readOnlyHint: false, destructiveHint: false, idempotentHint: true, openWorldHint: false },
|
|
52
|
+
};
|
|
53
|
+
export class TaskCommentArgumentError extends Error {
|
|
54
|
+
code = 'invalid_arguments';
|
|
55
|
+
}
|
|
56
|
+
function record(value) {
|
|
57
|
+
return value && typeof value === 'object' && !Array.isArray(value) ? value : null;
|
|
58
|
+
}
|
|
59
|
+
function requiredIdentifier(value, field) {
|
|
60
|
+
const trimmed = typeof value === 'string' ? value.trim() : '';
|
|
61
|
+
if (typeof value !== 'string' || !trimmed || value.length > TASK_ID_MAX_LENGTH || CONTROL.test(value)) {
|
|
62
|
+
throw new TaskCommentArgumentError(`${field} is invalid`);
|
|
63
|
+
}
|
|
64
|
+
return trimmed;
|
|
65
|
+
}
|
|
66
|
+
export function addTaskCommentRequest(args) {
|
|
67
|
+
const input = record(args);
|
|
68
|
+
if (!input)
|
|
69
|
+
throw new TaskCommentArgumentError('arguments must be an object');
|
|
70
|
+
const unsupported = Object.keys(input).filter(key => !['task_id', 'body', 'reply_to', 'idempotency_key'].includes(key));
|
|
71
|
+
if (unsupported.length)
|
|
72
|
+
throw new TaskCommentArgumentError(`unsupported argument: ${unsupported.join(', ')}`);
|
|
73
|
+
const taskId = requiredIdentifier(input.task_id, 'task_id');
|
|
74
|
+
if (taskId === '.' || taskId === '..')
|
|
75
|
+
throw new TaskCommentArgumentError('task_id is invalid');
|
|
76
|
+
if (typeof input.body !== 'string' || !input.body.trim()
|
|
77
|
+
|| input.body.length > MAX_COMMENT_BODY_LENGTH || BODY_CONTROL.test(input.body)) {
|
|
78
|
+
throw new TaskCommentArgumentError('body is invalid');
|
|
79
|
+
}
|
|
80
|
+
const idempotencyKey = requiredIdentifier(input.idempotency_key, 'idempotency_key');
|
|
81
|
+
return {
|
|
82
|
+
method: 'POST',
|
|
83
|
+
path: `/api/companion/tasks/${encodeURIComponent(taskId)}/comments`,
|
|
84
|
+
taskId,
|
|
85
|
+
body: {
|
|
86
|
+
body: input.body,
|
|
87
|
+
idempotency_key: idempotencyKey,
|
|
88
|
+
...(input.reply_to === undefined ? {} : { reply_to: requiredIdentifier(input.reply_to, 'reply_to') }),
|
|
89
|
+
},
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
/** The HTTP request a host sends for `list_task_comments`. Throws TaskCommentArgumentError on bad input. */
|
|
93
|
+
export function listTaskCommentsRequest(args) {
|
|
94
|
+
const input = record(args);
|
|
95
|
+
if (!input)
|
|
96
|
+
throw new TaskCommentArgumentError('arguments must be an object');
|
|
97
|
+
const unknown = Object.keys(input).filter((key) => key !== 'task_id' && key !== 'limit');
|
|
98
|
+
if (unknown.length)
|
|
99
|
+
throw new TaskCommentArgumentError(`unsupported argument: ${unknown.join(', ')}`);
|
|
100
|
+
const raw = typeof input.task_id === 'string' ? input.task_id : '';
|
|
101
|
+
const taskId = raw.trim();
|
|
102
|
+
if (!taskId || taskId.length > TASK_ID_MAX_LENGTH || CONTROL.test(raw))
|
|
103
|
+
throw new TaskCommentArgumentError('task_id is required');
|
|
104
|
+
let query = '';
|
|
105
|
+
if (input.limit !== undefined) {
|
|
106
|
+
const limit = input.limit;
|
|
107
|
+
if (typeof limit !== 'number' || !Number.isSafeInteger(limit) || limit < 1 || limit > LIST_TASK_COMMENTS_MAX_LIMIT) {
|
|
108
|
+
throw new TaskCommentArgumentError(`limit must be an integer from 1 to ${LIST_TASK_COMMENTS_MAX_LIMIT}`);
|
|
109
|
+
}
|
|
110
|
+
query = `?limit=${limit}`;
|
|
111
|
+
}
|
|
112
|
+
return { method: 'GET', path: `/api/companion/tasks/${encodeURIComponent(taskId)}/comments${query}`, taskId };
|
|
113
|
+
}
|
|
114
|
+
function comment(value) {
|
|
115
|
+
const item = record(value);
|
|
116
|
+
if (!item)
|
|
117
|
+
return null;
|
|
118
|
+
const optionalText = (key) => item[key] === null || typeof item[key] === 'string';
|
|
119
|
+
if (typeof item.id !== 'string' || !item.id || typeof item.body !== 'string'
|
|
120
|
+
|| !optionalText('reply_to') || !optionalText('author') || !optionalText('created_at') || !optionalText('edited_at')
|
|
121
|
+
|| typeof item.version !== 'number' || !Number.isSafeInteger(item.version) || item.version < 0)
|
|
122
|
+
return null;
|
|
123
|
+
return {
|
|
124
|
+
id: item.id,
|
|
125
|
+
body: item.body,
|
|
126
|
+
reply_to: item.reply_to,
|
|
127
|
+
author: item.author,
|
|
128
|
+
created_at: item.created_at,
|
|
129
|
+
edited_at: item.edited_at,
|
|
130
|
+
version: item.version,
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
function responseErrorCode(payload) {
|
|
134
|
+
const nested = record(payload?.error);
|
|
135
|
+
return typeof payload?.code === 'string' ? payload.code
|
|
136
|
+
: typeof nested?.code === 'string' ? nested.code : 'task_comments_unavailable';
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Checks the API answer for the requested Task. An error answer keeps only its
|
|
140
|
+
* code; any other body detail is dropped so a store message cannot reach the agent.
|
|
141
|
+
*/
|
|
142
|
+
export function readListTaskCommentsResponse(taskId, httpStatus, body) {
|
|
143
|
+
const payload = record(body);
|
|
144
|
+
if (httpStatus < 200 || httpStatus >= 300) {
|
|
145
|
+
const code = responseErrorCode(payload);
|
|
146
|
+
return { status: 'error', error: { code, message: `Task comments could not be read (${code})`, http_status: httpStatus } };
|
|
147
|
+
}
|
|
148
|
+
const comments = Array.isArray(payload?.comments) ? payload.comments.map(comment) : null;
|
|
149
|
+
if (!payload || payload.task_id !== taskId || !comments || comments.some((item) => item === null)
|
|
150
|
+
|| typeof payload.total_count !== 'number' || !Number.isSafeInteger(payload.total_count) || payload.total_count < comments.length
|
|
151
|
+
|| typeof payload.truncated !== 'boolean') {
|
|
152
|
+
return { status: 'error', error: { code: 'task_contract_error', message: 'Task comment response does not match the requested Task' } };
|
|
153
|
+
}
|
|
154
|
+
return {
|
|
155
|
+
status: 'ok',
|
|
156
|
+
page: { task_id: taskId, comments: comments, total_count: payload.total_count, truncated: payload.truncated },
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
export function readAddTaskCommentResponse(httpStatus, body) {
|
|
160
|
+
const payload = record(body);
|
|
161
|
+
if (httpStatus < 200 || httpStatus >= 300) {
|
|
162
|
+
const code = responseErrorCode(payload);
|
|
163
|
+
return { status: 'error', error: { code, message: `Task comment could not be posted (${code})`, http_status: httpStatus } };
|
|
164
|
+
}
|
|
165
|
+
const saved = comment(payload?.comment);
|
|
166
|
+
const notification = record(payload?.notification);
|
|
167
|
+
if (!payload || !saved || (httpStatus !== 200 && httpStatus !== 201) || payload?.replayed !== (httpStatus === 200)
|
|
168
|
+
|| notification?.status !== 'not_delivered' || notification.reason !== 'notification_delivery_not_configured') {
|
|
169
|
+
return { status: 'error', error: { code: 'task_contract_error', message: 'Task comment response is invalid' } };
|
|
170
|
+
}
|
|
171
|
+
return {
|
|
172
|
+
status: 'ok',
|
|
173
|
+
comment: saved,
|
|
174
|
+
replayed: payload.replayed,
|
|
175
|
+
notification: { status: notification.status, reason: notification.reason },
|
|
176
|
+
};
|
|
177
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { Tool } from '@modelcontextprotocol/sdk/types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Canonical Task `list_tasks` MCP contract.
|
|
4
|
+
*
|
|
5
|
+
* The host owns authentication, tenant binding, the HTTP client and the
|
|
6
|
+
* handling of non-2xx answers. This module owns the tool schema, the request
|
|
7
|
+
* it maps to on the companion task API, and the check of a page answer, so
|
|
8
|
+
* every host lists Tasks the same way instead of keeping its own copy.
|
|
9
|
+
*
|
|
10
|
+
* Archived Tasks are left out unless the caller passes include_archived: true.
|
|
11
|
+
* Only then is a single `include_archived=true` forwarded to the list or search
|
|
12
|
+
* endpoint; false or omitted sends the same request as before archiving existed.
|
|
13
|
+
*/
|
|
14
|
+
export declare const LIST_TASKS_TOOL_NAME = "list_tasks";
|
|
15
|
+
/** The task kernel caps list pages at 50 and full-text search pages at 20. */
|
|
16
|
+
export declare const LIST_TASKS_MAX_LIMIT = 50;
|
|
17
|
+
export declare const SEARCH_TASKS_MAX_LIMIT = 20;
|
|
18
|
+
export declare const listTasksTool: Tool;
|
|
19
|
+
export declare class TaskListArgumentError extends Error {
|
|
20
|
+
readonly code = "invalid_arguments";
|
|
21
|
+
}
|
|
22
|
+
export type ListTasksResult = {
|
|
23
|
+
status: 'ok';
|
|
24
|
+
page: Record<string, unknown>;
|
|
25
|
+
} | {
|
|
26
|
+
status: 'error';
|
|
27
|
+
error: {
|
|
28
|
+
code: 'task_contract_error';
|
|
29
|
+
message: string;
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* The HTTP request a host sends for `list_tasks`, relative to the companion
|
|
34
|
+
* task API base URL. Throws TaskListArgumentError before any request is made.
|
|
35
|
+
*/
|
|
36
|
+
export declare function listTasksRequest(args: unknown): {
|
|
37
|
+
method: 'GET';
|
|
38
|
+
path: string;
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* Checks a 2xx answer is a Task page and passes it through unchanged (archived_at and
|
|
42
|
+
* archive_reason included). Non-2xx answers stay with the host's common Task error mapping.
|
|
43
|
+
*/
|
|
44
|
+
export declare function readListTasksResponse(body: unknown): ListTasksResult;
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Canonical Task `list_tasks` MCP contract.
|
|
3
|
+
*
|
|
4
|
+
* The host owns authentication, tenant binding, the HTTP client and the
|
|
5
|
+
* handling of non-2xx answers. This module owns the tool schema, the request
|
|
6
|
+
* it maps to on the companion task API, and the check of a page answer, so
|
|
7
|
+
* every host lists Tasks the same way instead of keeping its own copy.
|
|
8
|
+
*
|
|
9
|
+
* Archived Tasks are left out unless the caller passes include_archived: true.
|
|
10
|
+
* Only then is a single `include_archived=true` forwarded to the list or search
|
|
11
|
+
* endpoint; false or omitted sends the same request as before archiving existed.
|
|
12
|
+
*/
|
|
13
|
+
export const LIST_TASKS_TOOL_NAME = 'list_tasks';
|
|
14
|
+
/** The task kernel caps list pages at 50 and full-text search pages at 20. */
|
|
15
|
+
export const LIST_TASKS_MAX_LIMIT = 50;
|
|
16
|
+
export const SEARCH_TASKS_MAX_LIMIT = 20;
|
|
17
|
+
const TASK_STATUSES = ['pending', 'in_progress', 'waiting', 'completed', 'cancelled'];
|
|
18
|
+
const TASK_PRIORITIES = ['low', 'medium', 'high', 'urgent'];
|
|
19
|
+
export const listTasksTool = {
|
|
20
|
+
name: LIST_TASKS_TOOL_NAME,
|
|
21
|
+
description: `List Canonical Tasks page by page (at most ${LIST_TASKS_MAX_LIMIT} per page, ${SEARCH_TASKS_MAX_LIMIT} when query is set). `
|
|
22
|
+
+ 'Follow nextCursor until it is null; totalCount and countStatus report whether the total is exact. '
|
|
23
|
+
+ 'Passing query switches to full-text search. '
|
|
24
|
+
+ 'Archived Tasks are left out unless include_archived is true; keep the same value while following nextCursor.',
|
|
25
|
+
inputSchema: {
|
|
26
|
+
type: 'object',
|
|
27
|
+
properties: {
|
|
28
|
+
query: { type: 'string', description: 'Optional full-text search query.' },
|
|
29
|
+
statuses: { type: 'array', items: { type: 'string', enum: [...TASK_STATUSES] } },
|
|
30
|
+
priorities: { type: 'array', items: { type: 'string', enum: [...TASK_PRIORITIES] } },
|
|
31
|
+
project_codes: { type: 'array', items: { type: 'string' } },
|
|
32
|
+
assignee_person_id: { type: 'string' },
|
|
33
|
+
due_after: { type: 'string', description: 'ISO 8601 date-time lower bound.' },
|
|
34
|
+
due_before: { type: 'string', description: 'ISO 8601 date-time upper bound.' },
|
|
35
|
+
limit: { type: 'integer', minimum: 1, maximum: LIST_TASKS_MAX_LIMIT },
|
|
36
|
+
cursor: { type: 'string' },
|
|
37
|
+
include_archived: {
|
|
38
|
+
type: 'boolean',
|
|
39
|
+
description: 'true also returns archived Tasks within the same scope, filters and paging. Omit or false for active Tasks only.',
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
// Only readOnlyHint, so a host that merges its own annotations lists the same tool as before.
|
|
44
|
+
annotations: { readOnlyHint: true },
|
|
45
|
+
};
|
|
46
|
+
export class TaskListArgumentError extends Error {
|
|
47
|
+
code = 'invalid_arguments';
|
|
48
|
+
}
|
|
49
|
+
function record(value) {
|
|
50
|
+
return value && typeof value === 'object' && !Array.isArray(value) ? value : null;
|
|
51
|
+
}
|
|
52
|
+
function optionalString(args, key) {
|
|
53
|
+
const value = args[key];
|
|
54
|
+
if (value === undefined || value === null || value === '')
|
|
55
|
+
return null;
|
|
56
|
+
if (typeof value !== 'string' || !value.trim())
|
|
57
|
+
throw new TaskListArgumentError(`${key} must be a non-empty string`);
|
|
58
|
+
return value.trim();
|
|
59
|
+
}
|
|
60
|
+
function optionalStringList(args, key) {
|
|
61
|
+
const value = args[key];
|
|
62
|
+
if (value === undefined || value === null)
|
|
63
|
+
return null;
|
|
64
|
+
if (!Array.isArray(value) || value.some((item) => typeof item !== 'string' || !item.trim())) {
|
|
65
|
+
throw new TaskListArgumentError(`${key} must be an array of non-empty strings`);
|
|
66
|
+
}
|
|
67
|
+
return value.length ? value.map((item) => item.trim()) : null;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* The HTTP request a host sends for `list_tasks`, relative to the companion
|
|
71
|
+
* task API base URL. Throws TaskListArgumentError before any request is made.
|
|
72
|
+
*/
|
|
73
|
+
export function listTasksRequest(args) {
|
|
74
|
+
const input = record(args);
|
|
75
|
+
if (!input)
|
|
76
|
+
throw new TaskListArgumentError('arguments must be an object');
|
|
77
|
+
const query = new URLSearchParams();
|
|
78
|
+
const text = optionalString(input, 'query');
|
|
79
|
+
if (text !== null)
|
|
80
|
+
query.set('q', text);
|
|
81
|
+
const lists = [
|
|
82
|
+
['statuses', 'statuses'], ['priorities', 'priorities'], ['project_codes', 'projectCodes'],
|
|
83
|
+
];
|
|
84
|
+
for (const [key, parameter] of lists) {
|
|
85
|
+
const values = optionalStringList(input, key);
|
|
86
|
+
if (values)
|
|
87
|
+
query.set(parameter, values.join(','));
|
|
88
|
+
}
|
|
89
|
+
const scalars = [
|
|
90
|
+
['assignee_person_id', 'assigneePersonId'], ['due_after', 'dueAfter'], ['due_before', 'dueBefore'], ['cursor', 'cursor'],
|
|
91
|
+
];
|
|
92
|
+
for (const [key, parameter] of scalars) {
|
|
93
|
+
const value = optionalString(input, key);
|
|
94
|
+
if (value !== null)
|
|
95
|
+
query.set(parameter, value);
|
|
96
|
+
}
|
|
97
|
+
if (input.limit !== undefined && input.limit !== null) {
|
|
98
|
+
const maxLimit = text !== null ? SEARCH_TASKS_MAX_LIMIT : LIST_TASKS_MAX_LIMIT;
|
|
99
|
+
if (typeof input.limit !== 'number' || !Number.isInteger(input.limit) || input.limit < 1 || input.limit > maxLimit) {
|
|
100
|
+
throw new TaskListArgumentError(`limit must be an integer from 1 to ${maxLimit}`);
|
|
101
|
+
}
|
|
102
|
+
query.set('limit', String(input.limit));
|
|
103
|
+
}
|
|
104
|
+
if (input.include_archived !== undefined && input.include_archived !== null) {
|
|
105
|
+
if (typeof input.include_archived !== 'boolean')
|
|
106
|
+
throw new TaskListArgumentError('include_archived must be a boolean');
|
|
107
|
+
// The task API includes archived Tasks only for exactly one include_archived=true.
|
|
108
|
+
if (input.include_archived)
|
|
109
|
+
query.set('include_archived', 'true');
|
|
110
|
+
}
|
|
111
|
+
const suffix = query.toString();
|
|
112
|
+
return {
|
|
113
|
+
method: 'GET',
|
|
114
|
+
path: `/api/companion/tasks${text !== null ? '/search' : ''}${suffix ? `?${suffix}` : ''}`,
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Checks a 2xx answer is a Task page and passes it through unchanged (archived_at and
|
|
119
|
+
* archive_reason included). Non-2xx answers stay with the host's common Task error mapping.
|
|
120
|
+
*/
|
|
121
|
+
export function readListTasksResponse(body) {
|
|
122
|
+
const page = record(body);
|
|
123
|
+
if (!page) {
|
|
124
|
+
return { status: 'error', error: { code: 'task_contract_error', message: 'Expected a Canonical Task object response' } };
|
|
125
|
+
}
|
|
126
|
+
if (!Array.isArray(page.items)) {
|
|
127
|
+
return { status: 'error', error: { code: 'task_contract_error', message: 'Expected a Canonical Task page response' } };
|
|
128
|
+
}
|
|
129
|
+
return { status: 'ok', page };
|
|
130
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unson/brainbase-mcp",
|
|
3
|
-
"version": "0.51.
|
|
3
|
+
"version": "0.51.10",
|
|
4
4
|
"description": "Local-first Brainbase judgment OS that lets supported AI agents reuse a user's approved goals, priorities, decisions, and judgment criteria.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -159,6 +159,14 @@
|
|
|
159
159
|
"types": "./dist/canonical-task-operations.d.ts",
|
|
160
160
|
"import": "./dist/canonical-task-operations.js"
|
|
161
161
|
},
|
|
162
|
+
"./canonical-task-comment-tools": {
|
|
163
|
+
"types": "./dist/canonical-task-comment-tools.d.ts",
|
|
164
|
+
"import": "./dist/canonical-task-comment-tools.js"
|
|
165
|
+
},
|
|
166
|
+
"./canonical-task-list-tools": {
|
|
167
|
+
"types": "./dist/canonical-task-list-tools.d.ts",
|
|
168
|
+
"import": "./dist/canonical-task-list-tools.js"
|
|
169
|
+
},
|
|
162
170
|
"./canonical-task-store-config": {
|
|
163
171
|
"types": "./dist/canonical-task-store-config.d.ts",
|
|
164
172
|
"import": "./dist/canonical-task-store-config.js"
|
|
@@ -673,5 +681,5 @@
|
|
|
673
681
|
"url": "https://github.com/Unson-LLC/brainbase/issues"
|
|
674
682
|
},
|
|
675
683
|
"homepage": "https://github.com/Unson-LLC/brainbase#readme",
|
|
676
|
-
"gitHead": "
|
|
684
|
+
"gitHead": "4f0cd113a8fe2048bf5c2882f2c9e1402b32497a"
|
|
677
685
|
}
|