feat(16-03): wire userId into MCP server and tool registrations

- Update createMcpServer signature to accept (db, userId)
- MCP auth middleware resolves userId from API key and Bearer token
- Store userId alongside transport in session map
- All 4 tool registration functions accept and pass userId
- Collection summary resource passes userId to all service calls
This commit is contained in:
2026-04-05 10:52:43 +02:00
parent e78002208a
commit d4bf4f5c16
6 changed files with 70 additions and 54 deletions

View File

@@ -113,13 +113,17 @@ export const threadToolDefinitions = [
},
];
export function registerThreadTools(db: Db) {
export function registerThreadTools(db: Db, userId: number) {
return {
list_threads: async (args: {
includeResolved?: boolean;
}): Promise<ToolResult> => {
try {
const threadList = await getAllThreads(db, args.includeResolved ?? false);
const threadList = await getAllThreads(
db,
userId,
args.includeResolved ?? false,
);
return textResult(threadList);
} catch (err) {
return errorResult((err as Error).message);
@@ -128,7 +132,7 @@ export function registerThreadTools(db: Db) {
get_thread: async (args: { id: number }): Promise<ToolResult> => {
try {
const thread = await getThreadWithCandidates(db, args.id);
const thread = await getThreadWithCandidates(db, userId, args.id);
if (!thread) return errorResult(`Thread ${args.id} not found`);
return textResult(thread);
} catch (err) {
@@ -141,7 +145,7 @@ export function registerThreadTools(db: Db) {
categoryId: number;
}): Promise<ToolResult> => {
try {
const thread = await createThread(db, args);
const thread = await createThread(db, userId, args);
return textResult(thread);
} catch (err) {
return errorResult((err as Error).message);
@@ -153,7 +157,12 @@ export function registerThreadTools(db: Db) {
candidateId: number;
}): Promise<ToolResult> => {
try {
const result = await resolveThread(db, args.threadId, args.candidateId);
const result = await resolveThread(
db,
userId,
args.threadId,
args.candidateId,
);
if (!result.success) {
return errorResult(result.error ?? "Failed to resolve thread");
}
@@ -177,7 +186,7 @@ export function registerThreadTools(db: Db) {
}): Promise<ToolResult> => {
try {
const { threadId, ...data } = args;
const candidate = await createCandidate(db, threadId, data);
const candidate = await createCandidate(db, userId, threadId, data);
return textResult(candidate);
} catch (err) {
return errorResult((err as Error).message);
@@ -200,7 +209,7 @@ export function registerThreadTools(db: Db) {
}): Promise<ToolResult> => {
try {
const { id, ...data } = args;
const candidate = await updateCandidate(db, id, data);
const candidate = await updateCandidate(db, userId, id, data);
if (!candidate) return errorResult(`Candidate ${id} not found`);
return textResult(candidate);
} catch (err) {
@@ -210,7 +219,7 @@ export function registerThreadTools(db: Db) {
remove_candidate: async (args: { id: number }): Promise<ToolResult> => {
try {
const candidate = await deleteCandidate(db, args.id);
const candidate = await deleteCandidate(db, userId, args.id);
if (!candidate) return errorResult(`Candidate ${args.id} not found`);
return textResult({ deleted: true, candidate });
} catch (err) {