Skip to content

MCP Sample Server #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 147 additions & 0 deletions app/mcp/[transport]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import { AuthError, verifyToken } from "@/lib/vercel/auth";
import {
createMcpHandler,
experimental_withMcpAuth as withMcpAuth,
} from "@vercel/mcp-adapter";
import {
InvalidTokenError,
UnauthorizedClientError,
} from "@modelcontextprotocol/sdk/server/auth/errors.js";
import { z } from "zod";
import { listResources } from "@/lib/partner";

export const maxDuration = 300;

const _handler = createMcpHandler(
(server) => {
server.tool(
"greet",
"Greet a person!",
{ name: z.string().describe("The name of a person to greet") },
{
title: "Greet tool",
idempotentHint: true,
},
async ({ name }, extra) => {
return {
content: [
{
type: "text",
text: `Hello ${name} ${String(
extra.authInfo?.extra?.user_name || ""
).trim()}`,
},
],
};
}
);
server.tool(
"getResources",
"Get installation resources",
{},
{
title: "Get installation resources",
},
async (_, extra) => {
if (!extra.authInfo) {
throw new UnauthorizedClientError("Not authorized");
}
const installationId = extra.authInfo.clientId;
const { resources } = await listResources(installationId);
return {
content: [
{
type: "text" as const,
text: `Found ${resources.length} resources for installation ${installationId}`,
},
...resources.map((resource) => ({
type: "text" as const,
text: `Resource: ${resource.name} (${resource.id})`,
})),
],
};
}
);
server.tool(
"getResourceById",
"Get installation resource by ID",
{ id: z.string().describe("The ID of the resource to retrieve") },
{
title: "Get installation resource by ID",
vercel: {
resourceIdArg: "id",
},
},
async ({ id }, extra) => {
if (!extra.authInfo) {
throw new UnauthorizedClientError("Not authorized");
}
const installationId = extra.authInfo.clientId;
const { resources } = await listResources(installationId, [id]);
const resource = resources.find((r) => r.id === id);
return {
content: [
{
type: "text" as const,
text: resource
? `Resource found: ${resource.name} (${resource.id})`
: `Resource with ID ${id} not found`,
},
],
structuredContent: resource,
};
}
);
},
{
capabilities: {
tools: {
getResources: {
description: "Returns a list of resources for the installation",
vercel: {},
},
getResourceById: {
description: "Returns a specific resource by ID for the installation",
vercel: {
resourceIdArg: "id",
},
},
},
vercel: {},
},
},
{
// redis://redis:6379
redisUrl: process.env.KV_URL,
basePath: "/mcp",
verboseLogs: true,
maxDuration: 60,
}
);

const handler = withMcpAuth(
_handler,
async (_req, bearerToken) => {
if (!bearerToken) {
return undefined;
}
try {
const token = await verifyToken(bearerToken);
return {
token: bearerToken,
clientId: token.installation_id,
scopes: [token.user_role || "USER"],
extra: token,
};
} catch (err) {
if (err instanceof AuthError) {
console.error("Auth error:", err);
throw new InvalidTokenError(err.message);
}
throw err;
}
},
{ required: false }
);

export { handler as GET, handler as POST, handler as DELETE };
2 changes: 1 addition & 1 deletion lib/vercel/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,4 @@ function getAuthorizationToken(req: Request): string {
return match[1];
}

class AuthError extends Error {}
export class AuthError extends Error {}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
},
"dependencies": {
"@vercel/kv": "^1.0.1",
"@vercel/mcp-adapter": "^0.8.2",
"@modelcontextprotocol/sdk": "^1.12.0",
"jose": "^5.2.4",
"lodash": "^4.17.21",
"nanoid": "^5.0.7",
"next": "14.2.6",
"react": "^18",
"react-dom": "^18",
"zod": "^3.22.4"
"zod": "^3.24.2"
},
"devDependencies": {
"@types/lodash": "^4.17.0",
Expand Down
Loading