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

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