Add MCP server so Claude and ChatGPT can read and write drink data
Exposes the collection over the Model Context Protocol at /api/mcp, with 22 tools covering drinks, ratings, bar inventory, recipes, wishlist and taste preferences, plus search/fetch aliases for ChatGPT's deep-research mode. Authentication is a bearer token, the app's first header-borne credential - every other route derives identity from the NextAuth cookie, which a machine client cannot present. /api/mcp sits under the middleware's /api exclusion so it can answer a JSON 401 with an RFC 9728 WWW-Authenticate challenge instead of an HTML redirect to /login. Tokens are stored as a SHA-256 hash rather than plaintext like Invite.token and PasswordReset.token. Those are single-use and short-lived; this one is long-lived and grants read/write over a whole collection, and the nightly pg_dump keeps 14 days of history. Not encrypt(), which is reversible AES and right only for outbound keys we must replay; not bcrypt, which cannot be indexed and would turn verification into a table scan per request. verifyMcpToken joins User.status on every call, mirroring the jwt callback, so suspending a member kills their MCP access immediately rather than leaving the token as a documented way to outlive suspension. It fails closed on a database error, deliberately unlike the jwt callback, which keeps the session because a throw there would sign out every user at once. No tool reaches the Switchboard gateway. Claude and ChatGPT are language models already, so they can reason over a bar inventory without the app paying to do it a second time, and a remote client looping a vision call is not a failure mode worth having. Account deletion, restore, gateway keys, admin routes and shared-list creation are excluded too. The OAuth models ship now but are unused; the token endpoint will write the same McpAccessToken rows, so adding it later touches no verification code. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01W1Ee4Mc1X1SX8HgYa52zu7
This commit is contained in:
61
src/lib/mcp/config.ts
Normal file
61
src/lib/mcp/config.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
/**
|
||||
* Static configuration for the MCP server.
|
||||
*
|
||||
* The origin is read from NEXTAUTH_URL rather than derived from the incoming
|
||||
* request. `publicOrigin()` in src/lib/origin.ts explains why for links; here it
|
||||
* matters more. RFC 9728 requires the `resource` field of the protected resource
|
||||
* metadata to match the URL the user typed into their client byte for byte, and
|
||||
* RFC 8414 requires the OAuth `issuer` to equal the origin it was discovered at.
|
||||
* Deriving either from X-Forwarded-* means a proxy misconfiguration shows up as a
|
||||
* silent connector failure with no useful error, so it is pinned to config instead.
|
||||
*/
|
||||
|
||||
export const MCP_SERVER_NAME = "drinktracker"
|
||||
export const MCP_SERVER_VERSION = "1.0.0"
|
||||
|
||||
/** Path the MCP endpoint is mounted at. Clients are given the full URL. */
|
||||
export const MCP_PATH = "/api/mcp"
|
||||
|
||||
/**
|
||||
* Scopes. Two axes, read and write, over two halves of the data:
|
||||
*
|
||||
* drinks:* the collection and journal - drinks, ratings, wishlist, preferences
|
||||
* bar:* the inventory and what can be made from it - bar items, recipes
|
||||
*
|
||||
* Kept deliberately coarse. A consent screen listing eight scopes is a consent
|
||||
* screen nobody reads.
|
||||
*/
|
||||
export const MCP_SCOPES = [
|
||||
"drinks:read",
|
||||
"drinks:write",
|
||||
"bar:read",
|
||||
"bar:write",
|
||||
] as const
|
||||
|
||||
export type McpScope = (typeof MCP_SCOPES)[number]
|
||||
|
||||
/** Everything a read-only connection gets. The default when minting a token. */
|
||||
export const MCP_READ_SCOPES: McpScope[] = ["drinks:read", "bar:read"]
|
||||
|
||||
/** Full access. */
|
||||
export const MCP_ALL_SCOPES: McpScope[] = [...MCP_SCOPES]
|
||||
|
||||
export function isMcpScope(value: string): value is McpScope {
|
||||
return (MCP_SCOPES as readonly string[]).includes(value)
|
||||
}
|
||||
|
||||
/** Public origin, no trailing slash. */
|
||||
export function mcpBaseUrl(): string {
|
||||
const configured = process.env.NEXTAUTH_URL
|
||||
if (configured) return configured.replace(/\/$/, "")
|
||||
// Only reachable in local development, where NEXTAUTH_URL is usually unset.
|
||||
return "http://localhost:3000"
|
||||
}
|
||||
|
||||
/**
|
||||
* The resource identifier for this MCP server, which is also the URL a user
|
||||
* pastes into claude.ai. Must match their input exactly.
|
||||
*/
|
||||
export function mcpResourceUrl(): string {
|
||||
return `${mcpBaseUrl()}${MCP_PATH}`
|
||||
}
|
||||
Reference in New Issue
Block a user