Skip to main content
This guide will walk you through the process of building an OAuth-compatible MCP client in Typescript. We’ll be using Next.js and the Model Context Protocol SDK. It builds off of the official example from the MCP TypeScript SDK.

Show me the repo

View the fully runnable GitHub repo for this example

Official MCP Docs

View the official MCP docs for authorization

1. Install dependencies

First, create a new Next.js project and install the MCP TypeScript SDK.

2. Create Library Files

You’ll want to create a /lib directory for common code across your application. This will contain the core logic for the MCP client and the authentication flow. Then, create the following files in your /lib directory:

Session Store

This file contains a simple in-memory session store. For production applications, you should use a more robust solution like Redis or a database.
/lib/session-store.ts

OAuth Client

This file contains the core logic for the MCP OAuth client.
/lib/oauth-client.ts

3. Create OAuth API Routes

Next, you’ll want to create a few API routes to handle the OAuth flow. Create the following API routes in a directory at /app/api/mcp:
  • /app/api/mcp/auth/connect - Initiates the connection to the MCP server.
  • /app/api/mcp/auth/callback - Handles the OAuth callback from the MCP server.
  • /app/api/mcp/auth/finish - Finalizes the OAuth flow and stores the tokens.
  • /app/api/mcp/auth/disconnect - Disconnects from the MCP server.

Initialize the OAuth flow

This endpoint initiates the connection to the MCP server.
/app/api/mcp/auth/connect/route.ts

Handle the OAuth callback

This is the OAuth callback endpoint.
/app/api/mcp/auth/callback/route.ts

Finalize the OAuth flow

This endpoint finalizes the OAuth flow.
/app/api/mcp/auth/finish/route.ts

Disconnect from the MCP server

This endpoint disconnects from the MCP server.
/app/api/mcp/auth/disconnect/route.ts

4. List/Call Tools

Next, you’ll want to create a few API routes to handle actually using the tools provided by the MCP server. Create the following API routes in a directory at /app/api/mcp:
  • /app/api/mcp/tool/list - Lists the available tools on the MCP server.
  • /app/api/mcp/tool/call - Calls a tool on the MCP server.

List the available tools

This endpoint lists the available tools on the MCP server.
/app/api/mcp/tool/list/route.ts

Call a tool

This endpoint calls a tool on the MCP server.
/app/api/mcp/tool/call/route.ts

5. Build the UI

Now, you’ll want to build the UI to call these endpoints. You can use the following code to get started:
/app/page.tsx

Putting it all together

With these files in place, your Next.js application can now interact with an MCP server.

Authentication Endpoints

  • Connect: POST /api/mcp/auth/connect with serverUrl and callbackUrl in the body. The callbackUrl should point to /api/mcp/auth/callback.
  • List Tools: GET /api/mcp/tool/list?sessionId=<sessionId>
  • Call Tool: POST /api/mcp/tool/call with toolName, toolArgs, and sessionId in the body.
  • Disconnect: POST /api/mcp/auth/disconnect with sessionId in the body.

Tool Endpoints

  • List Tools: GET /api/mcp/tool/list?sessionId=<sessionId>
  • Call Tool: POST /api/mcp/tool/call with toolName, toolArgs, and sessionId in the body.
Here’s what your directory structure should look like: