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

@@ -37,11 +37,11 @@ export const categoryToolDefinitions = [
},
];
export function registerCategoryTools(db: Db) {
export function registerCategoryTools(db: Db, userId: number) {
return {
list_categories: async (): Promise<ToolResult> => {
try {
const cats = await getAllCategories(db);
const cats = await getAllCategories(db, userId);
return textResult(cats);
} catch (err) {
return errorResult((err as Error).message);
@@ -53,7 +53,7 @@ export function registerCategoryTools(db: Db) {
icon?: string;
}): Promise<ToolResult> => {
try {
const cat = await createCategory(db, args);
const cat = await createCategory(db, userId, args);
return textResult(cat);
} catch (err) {
return errorResult((err as Error).message);

View File

@@ -91,11 +91,11 @@ export const itemToolDefinitions = [
},
];
export function registerItemTools(db: Db) {
export function registerItemTools(db: Db, userId: number) {
return {
list_items: async (args: { categoryId?: number }): Promise<ToolResult> => {
try {
const items = await getAllItems(db);
const items = await getAllItems(db, userId);
if (args.categoryId) {
return textResult(
items.filter((i) => i.categoryId === args.categoryId),
@@ -109,7 +109,7 @@ export function registerItemTools(db: Db) {
get_item: async (args: { id: number }): Promise<ToolResult> => {
try {
const item = await getItemById(db, args.id);
const item = await getItemById(db, userId, args.id);
if (!item) return errorResult(`Item ${args.id} not found`);
return textResult(item);
} catch (err) {
@@ -128,7 +128,7 @@ export function registerItemTools(db: Db) {
imageSourceUrl?: string;
}): Promise<ToolResult> => {
try {
const item = await createItem(db, args);
const item = await createItem(db, userId, args);
return textResult(item);
} catch (err) {
return errorResult((err as Error).message);
@@ -148,7 +148,7 @@ export function registerItemTools(db: Db) {
}): Promise<ToolResult> => {
try {
const { id, ...data } = args;
const item = await updateItem(db, id, data);
const item = await updateItem(db, userId, id, data);
if (!item) return errorResult(`Item ${id} not found`);
return textResult(item);
} catch (err) {
@@ -158,7 +158,7 @@ export function registerItemTools(db: Db) {
delete_item: async (args: { id: number }): Promise<ToolResult> => {
try {
const item = await deleteItem(db, args.id);
const item = await deleteItem(db, userId, args.id);
if (!item) return errorResult(`Item ${args.id} not found`);
return textResult({ deleted: true, item });
} catch (err) {

View File

@@ -60,11 +60,11 @@ export const setupToolDefinitions = [
},
];
export function registerSetupTools(db: Db) {
export function registerSetupTools(db: Db, userId: number) {
return {
list_setups: async (): Promise<ToolResult> => {
try {
const setupList = await getAllSetups(db);
const setupList = await getAllSetups(db, userId);
return textResult(setupList);
} catch (err) {
return errorResult((err as Error).message);
@@ -73,7 +73,7 @@ export function registerSetupTools(db: Db) {
get_setup: async (args: { id: number }): Promise<ToolResult> => {
try {
const setup = await getSetupWithItems(db, args.id);
const setup = await getSetupWithItems(db, userId, args.id);
if (!setup) return errorResult(`Setup ${args.id} not found`);
return textResult(setup);
} catch (err) {
@@ -83,7 +83,7 @@ export function registerSetupTools(db: Db) {
create_setup: async (args: { name: string }): Promise<ToolResult> => {
try {
const setup = await createSetup(db, args);
const setup = await createSetup(db, userId, args);
return textResult(setup);
} catch (err) {
return errorResult((err as Error).message);
@@ -98,14 +98,14 @@ export function registerSetupTools(db: Db) {
try {
let setup = null;
if (args.name) {
setup = await updateSetup(db, args.id, { name: args.name });
setup = await updateSetup(db, userId, args.id, { name: args.name });
if (!setup) return errorResult(`Setup ${args.id} not found`);
}
if (args.itemIds) {
await syncSetupItems(db, args.id, args.itemIds);
await syncSetupItems(db, userId, args.id, args.itemIds);
}
// Return updated setup with items
const result = await getSetupWithItems(db, args.id);
const result = await getSetupWithItems(db, userId, args.id);
if (!result) return errorResult(`Setup ${args.id} not found`);
return textResult(result);
} catch (err) {

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) {