Skip to content

Commit 488dfe4

Browse files
authored
.changeset/evil-things-check.md README.md package.json pnpm-lock.yaml src/auth/auth-context.ts src/auth/auth-metadata.ts src/auth/auth-wrapper.ts src/handler/index.ts src/handler/mcp-api-handler.ts src/handler/server-response-adapter.ts src/index.ts (#78)
1 parent 02df830 commit 488dfe4

File tree

11 files changed

+57
-36
lines changed

11 files changed

+57
-36
lines changed

.changeset/evil-things-check.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@vercel/mcp-adapter": minor
3+
---
4+
5+
Refactor packaging and make withMcpAuth stable

README.md

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
A Vercel adapter for the Model Context Protocol (MCP), enabling real-time communication between your applications and AI models. Currently supports Next.js with more framework adapters coming soon.
44

5-
65
## Installation
76

87
```bash
@@ -154,13 +153,13 @@ interface Config {
154153

155154
## Authorization
156155

157-
The MCP adapter supports the [MCP Authorization Specification](https://modelcontextprotocol.io/specification/draft/basic/authorization) per the through the `experimental_withMcpAuth` wrapper. This allows you to protect your MCP endpoints and access authentication information in your tools.
156+
The MCP adapter supports the [MCP Authorization Specification](https://modelcontextprotocol.io/specification/draft/basic/authorization) per the through the `withMcpAuth` wrapper. This allows you to protect your MCP endpoints and access authentication information in your tools.
158157

159158
### Basic Usage
160159

161160
```typescript
162161
// app/api/[transport]/route.ts
163-
import { createMcpHandler, experimental_withMcpAuth } from "@vercel/mcp-adapter";
162+
import { createMcpHandler, withMcpAuth } from "@vercel/mcp-adapter";
164163

165164
// Create your handler as normal
166165
const handler = createMcpHandler(
@@ -172,10 +171,16 @@ const handler = createMcpHandler(
172171
async ({ message }, extra) => {
173172
// Access auth info in your tools via extra.authInfo
174173
return {
175-
content: [{
176-
type: "text",
177-
text: `Echo: ${message}${extra.authInfo?.token ? ` for user ${extra.authInfo.clientId}` : ''}`
178-
}],
174+
content: [
175+
{
176+
type: "text",
177+
text: `Echo: ${message}${
178+
extra.authInfo?.token
179+
? ` for user ${extra.authInfo.clientId}`
180+
: ""
181+
}`,
182+
},
183+
],
179184
};
180185
}
181186
);
@@ -186,31 +191,35 @@ const handler = createMcpHandler(
186191
);
187192

188193
// Wrap your handler with authorization
189-
const verifyToken = async (req: Request, bearerToken?: string): Promise<AuthInfo | undefined> => {
194+
const verifyToken = async (
195+
req: Request,
196+
bearerToken?: string
197+
): Promise<AuthInfo | undefined> => {
190198
if (!bearerToken) return undefined;
191199

192200
// Replace this example with actual token verification logic
193201
// Return an AuthInfo object if verification succeeds
194202
// Otherwise, return undefined
195-
const isValid = bearerToken.startsWith('__TEST_VALUE__');
196-
203+
const isValid = bearerToken.startsWith("__TEST_VALUE__");
204+
197205
if (!isValid) return undefined;
198-
206+
199207
return {
200208
token: bearerToken,
201209
scopes: ["read:stuff"], // Add relevant scopes
202-
clientId: "user123", // Add user/client identifier
203-
extra: { // Optional extra information
204-
userId: "123"
205-
}
210+
clientId: "user123", // Add user/client identifier
211+
extra: {
212+
// Optional extra information
213+
userId: "123",
214+
},
206215
};
207216
};
208217

209218
// Make authorization required
210-
const authHandler = experimental_withMcpAuth(handler, verifyToken, {
211-
required: true, // Make auth required for all requests
212-
requiredScopes: ["read:stuff"], // Optional: Require specific scopes
213-
resourceMetadataPath: "/.well-known/oauth-protected-resource" // Optional: Custom metadata path
219+
const authHandler = withMcpAuth(handler, verifyToken, {
220+
required: true, // Make auth required for all requests
221+
requiredScopes: ["read:stuff"], // Optional: Require specific scopes
222+
resourceMetadataPath: "/.well-known/oauth-protected-resource", // Optional: Custom metadata path
214223
});
215224

216225
export { authHandler as GET, authHandler as POST };
@@ -226,17 +235,18 @@ Create a new file at `app/.well-known/oauth-protected-resource/route.ts`:
226235
import {
227236
protectedResourceHandler,
228237
metadataCorsOptionsRequestHandler,
229-
} from '@vercel/mcp-adapter'
238+
} from "@vercel/mcp-adapter";
230239

231240
const handler = protectedResourceHandler({
232-
// Specify the Issuer URL of the associated Authorization Server
233-
authServerUrls: ["https://auth-server.com"]
234-
})
241+
// Specify the Issuer URL of the associated Authorization Server
242+
authServerUrls: ["https://auth-server.com"],
243+
});
235244

236-
export { handler as GET, metadataCorsOptionsRequestHandler as OPTIONS }
245+
export { handler as GET, metadataCorsOptionsRequestHandler as OPTIONS };
237246
```
238247

239248
This endpoint provides:
249+
240250
- `resource`: The URL of your MCP server
241251
- `authorization_servers`: Array of OAuth authorization server Issuer URLs that can issue valid tokens
242252

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020
},
2121
"./next": {
2222
"types": {
23-
"import": "./dist/next/index.d.mts",
24-
"require": "./dist/next/index.d.cjs"
23+
"import": "./dist/index.d.mts",
24+
"require": "./dist/index.d.cjs"
2525
},
26-
"import": "./dist/next/index.mjs",
27-
"require": "./dist/next/index.js"
26+
"import": "./dist/index.mjs",
27+
"require": "./dist/index.js"
2828
}
2929
},
3030
"files": [

pnpm-lock.yaml

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/next/mcp-api-handler.ts renamed to src/handler/mcp-api-handler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import type {
2020
import { createEvent } from "../lib/log-helper";
2121
import { EventEmittingResponse } from "../lib/event-emitter.js";
2222
import { AuthInfo } from "@modelcontextprotocol/sdk/server/auth/types";
23-
import { getAuthContext } from "./auth-context";
23+
import { getAuthContext } from "../auth/auth-context";
2424
import { ServerOptions } from ".";
2525

2626
interface SerializedRequest {
File renamed without changes.

0 commit comments

Comments
 (0)