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

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