Files
shumai/packages/api/src/operation-support.ts
T
Yiling-J 4d61786799 feat(mcp): serve Operations over MCP and a generic /api/ops endpoint (#495)
Add a stateless Streamable HTTP MCP server at /api/mcp that exposes every
mcp-surface Operation with its input/output schemas and annotations, and
POST /api/ops/:name for the CLI. Both require an API token (session
cookies are rejected), run Operations as the token's user, honor demo
read-only mode, and record via mcp/cli/api in audit logs.

Also make list_projects toggleable in agent settings and document MCP
client setup.
2026-10-27 08:09:09 +08:00

36 lines
1.5 KiB
TypeScript

import { HTTPException } from 'hono/http-exception'
import type { ContentfulStatusCode } from 'hono/utils/http-status'
import type { Operation } from '@shumai/core/src/operations'
import { teamService } from '@shumai/core/src/team/team'
import { logger } from '@shumai/core/src/logger'
/**
* Map an error thrown while running an Operation to a status and a message that is safe to
* return to an external caller. `HTTPException`s and plain `Error`s raised by services carry
* user-facing messages; anything else (e.g. database driver errors) is logged and hidden.
*/
export function describeOperationError(
operation: string,
err: unknown,
): { status: ContentfulStatusCode; message: string } {
if (err instanceof HTTPException) {
return { status: err.status as ContentfulStatusCode, message: err.message }
}
logger.error({ err, operation }, 'Operation failed')
if (err instanceof Error && err.constructor === Error) {
return { status: 500, message: err.message }
}
return { status: 500, message: 'Internal server error' }
}
/**
* Mirrors the read-only demo mode enforced by `authMiddleware` for session requests:
* Operations that change data are rejected for users without a writable role in any team.
*/
export async function assertWritableInDemoMode(op: Operation, userId: string): Promise<void> {
if (process.env.SHUMAI_DEMO_MODE !== '1' || op.definition.annotations.readOnly) return
if (!(await teamService.hasWritableRoleInAnyTeam(userId))) {
throw new HTTPException(403, { message: 'System is in read-only mode' })
}
}