Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Serve OpenAPI with OAuth security scheme at /?mcp

Kiran K97526b557d5d
diff --git a/apps/web/app/api/route.ts b/apps/web/app/api/route.ts
index b3149410a1..b53ca14a43 100644
--- a/apps/web/app/api/route.ts
+++ b/apps/web/app/api/route.ts
@@ -1,10 +1,12 @@
import { document } from "@/lib/openapi";
-import { NextResponse } from "next/server";
+import { getMcpDocument } from "@/lib/openapi/mcp-document";
+import { NextRequest, NextResponse } from "next/server";
-export const dynamic = "force-static";
+export function GET(req: NextRequest) {
+ const isMcp = req.nextUrl.searchParams.has("mcp");
+ const openapiDocument = isMcp ? getMcpDocument() : document;
-export function GET() {
- return NextResponse.json(document, {
+ return NextResponse.json(openapiDocument, {
headers: {
// cache indefinitely till next deployment
"Vercel-CDN-Cache-Control": "s-maxage=31536000",
diff --git a/apps/web/lib/openapi/mcp-document.ts b/apps/web/lib/openapi/mcp-document.ts
new file mode 100644
index 0000000000..bee9aa0618
--- /dev/null
+++ b/apps/web/lib/openapi/mcp-document.ts
@@ -0,0 +1,61 @@
+import { document } from ".";
+import { OAUTH_SCOPE_DESCRIPTIONS, OAUTH_SCOPES } from "../api/oauth/constants";
+
+// OpenAPI document served at `api.dub.co/?mcp` for MCP clients.
+// Copies the public spec, adds an OAuth 2.0 (authorization code) security scheme
+// with the scopes from OAUTH_SCOPES, and accepts either the API key (`token`) or
+// OAuth at the document level. Operation-level security is left unchanged.
+//
+// Example (truncated):
+// {
+// "openapi": "3.0.3",
+// "paths": { ... },
+// "components": {
+// "securitySchemes": {
+// "token": { "type": "http", "scheme": "bearer", ... },
+// "oauth": {
+// "type": "oauth2",
+// "flows": {
+// "authorizationCode": {
+// "authorizationUrl": "https://app.dub.co/oauth/authorize",
+// "tokenUrl": "https://api.dub.co/oauth/token",
+// "scopes": { "links.read": "Read access to links", ... }
+// }
+// }
+// }
+// }
+// },
+// "security": [{ "token": [] }, { "oauth": [] }]
+// }
+export function getMcpDocument(): typeof document {
+ const mcpDocument = structuredClone(document);
+
+ mcpDocument.components = {
+ ...mcpDocument.components,
+ securitySchemes: {
+ ...mcpDocument.components?.securitySchemes,
+ oauth: {
+ type: "oauth2",
+ description: "OAuth 2.0 authentication",
+ flows: {
+ authorizationCode: {
+ authorizationUrl: "https://app.dub.co/oauth/authorize",
+ tokenUrl: "https://api.dub.co/oauth/token",
+ scopes: Object.fromEntries(
+ OAUTH_SCOPES.map((scope) => [
+ scope,
+ OAUTH_SCOPE_DESCRIPTIONS[
+ scope as keyof typeof OAUTH_SCOPE_DESCRIPTIONS
+ ] ?? scope,
+ ]),
+ ),
+ },
+ },
+ },
+ },
+ };
+
+ mcpDocument.security = [{ token: [] }, { oauth: [] }];
+
+ return mcpDocument;
+}