|
| 1 | +# MCP Server Setup for fullstack_demo |
| 2 | + |
| 3 | +This document explains how to use the Model Context Protocol (MCP) server that has been integrated into the fullstack_demo application. This allows AI agents like Claude Desktop to securely interact with your todos through a standardized protocol. |
| 4 | + |
| 5 | +## What is MCP? |
| 6 | + |
| 7 | +Model Context Protocol (MCP) is a standardized way for AI assistants to securely connect to external tools and services. This implementation allows Claude Desktop and other MCP-compatible AI agents to: |
| 8 | +- View all your todos |
| 9 | +- Create new todos |
| 10 | +- Update existing todos |
| 11 | +- Delete todos |
| 12 | +- Access your Clerk user data |
| 13 | + |
| 14 | +All operations are authenticated using Clerk OAuth tokens, ensuring secure access to your data. |
| 15 | + |
| 16 | +## Architecture |
| 17 | + |
| 18 | +The MCP server is built using: |
| 19 | +- **@vercel/mcp-adapter**: Handles the core MCP protocol logic |
| 20 | +- **@clerk/mcp-tools**: Integrates Clerk OAuth authentication with MCP |
| 21 | +- **Next.js App Router**: Provides the API endpoints |
| 22 | + |
| 23 | +### Key Components |
| 24 | + |
| 25 | +1. **MCP Route Handler** (`src/app/[transport]/route.ts`) |
| 26 | + - Defines MCP tools for todo operations |
| 27 | + - Supports both `/mcp` (HTTP) and `/sse` (Server-Sent Events) transports |
| 28 | + - Authenticates requests using Clerk OAuth tokens |
| 29 | + |
| 30 | +2. **OAuth Metadata Endpoints** |
| 31 | + - `/.well-known/oauth-protected-resource/mcp` - Resource metadata |
| 32 | + - `/.well-known/oauth-authorization-server` - Authorization server metadata (backward compatibility) |
| 33 | + |
| 34 | +3. **Middleware** (`src/middleware.ts`) |
| 35 | + - Updated to allow public access to `.well-known` endpoints |
| 36 | + - Maintains authentication for all other routes |
| 37 | + |
| 38 | +## Setup Instructions |
| 39 | + |
| 40 | +### 1. Enable Dynamic Client Registration in Clerk |
| 41 | + |
| 42 | +**IMPORTANT**: Before the MCP server can work, you must enable dynamic client registration in your Clerk Dashboard. |
| 43 | + |
| 44 | +1. Go to the [Clerk Dashboard](https://dashboard.clerk.com/) |
| 45 | +2. Navigate to **Configure** → **OAuth Applications** |
| 46 | +3. Toggle on **Dynamic client registration** |
| 47 | + |
| 48 | +This allows MCP-compatible clients to automatically register themselves during the OAuth flow. |
| 49 | + |
| 50 | +### 2. Start the Development Server |
| 51 | + |
| 52 | +```bash |
| 53 | +cd /Users/anthonymays/source/forks/code-society-25-2/lib/javascript/fullstack_demo |
| 54 | +npm run dev |
| 55 | +``` |
| 56 | + |
| 57 | +The server will start on `http://localhost:3000` (or your configured port). |
| 58 | + |
| 59 | +### 3. Connect AI Agents to Your MCP Server |
| 60 | + |
| 61 | +#### Recommended Method: Using mcp-remote |
| 62 | + |
| 63 | +The easiest way to connect AI agents to your MCP server is using the `mcp-remote` utility: |
| 64 | + |
| 65 | +```bash |
| 66 | +npx -y mcp-remote http://localhost:3000/mcp |
| 67 | +``` |
| 68 | + |
| 69 | +This command: |
| 70 | +- Automatically configures the MCP client |
| 71 | +- Handles OAuth authentication with Clerk |
| 72 | +- Works with any MCP-compatible AI agent |
| 73 | +- Requires no manual configuration file editing |
| 74 | + |
| 75 | +#### Alternative Method: Manual Configuration |
| 76 | + |
| 77 | +If you prefer to manually configure your AI agent: |
| 78 | + |
| 79 | +1. Locate your Claude Desktop configuration file: |
| 80 | + - **macOS**: `~/Library/Application Support/Claude/claude_desktop_config.json` |
| 81 | + - **Windows**: `%APPDATA%\Claude\claude_desktop_config.json` |
| 82 | + |
| 83 | +2. Add your MCP server configuration: |
| 84 | + |
| 85 | +```json |
| 86 | +{ |
| 87 | + "mcpServers": { |
| 88 | + "fullstack_demo_todos": { |
| 89 | + "url": "http://localhost:3000/mcp", |
| 90 | + "transport": "streamable-http", |
| 91 | + "auth": { |
| 92 | + "type": "oauth2", |
| 93 | + "authorizationUrl": "http://localhost:3000/.well-known/oauth-authorization-server" |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +**Note**: The `tokenUrl` is automatically discovered from the authorization server metadata. |
| 101 | + |
| 102 | +3. Restart your AI agent |
| 103 | + |
| 104 | +4. When the agent tries to use the MCP server, you'll be prompted to authenticate via Clerk OAuth. |
| 105 | + |
| 106 | +### 4. Production Deployment |
| 107 | + |
| 108 | +When deploying to production: |
| 109 | + |
| 110 | +1. Connect to your production MCP server using `mcp-remote`: |
| 111 | + ```bash |
| 112 | + npx -y mcp-remote https://your-production-domain.com/mcp |
| 113 | + ``` |
| 114 | + |
| 115 | + Or update your AI agent's configuration manually: |
| 116 | + ```json |
| 117 | + { |
| 118 | + "mcpServers": { |
| 119 | + "fullstack_demo_todos": { |
| 120 | + "url": "https://your-production-domain.com/mcp", |
| 121 | + "transport": "streamable-http", |
| 122 | + "auth": { |
| 123 | + "type": "oauth2", |
| 124 | + "authorizationUrl": "https://your-production-domain.com/.well-known/oauth-authorization-server" |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + ``` |
| 130 | + |
| 131 | +2. Ensure your production environment has all required environment variables set: |
| 132 | + - `NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY` |
| 133 | + - `CLERK_SECRET_KEY` |
| 134 | + - Database connection strings |
| 135 | + - Any other app-specific variables |
| 136 | + |
| 137 | +## Available MCP Tools |
| 138 | + |
| 139 | +### 1. `get-todos` |
| 140 | +Gets all todos for the authenticated user. |
| 141 | + |
| 142 | +**Parameters**: None |
| 143 | + |
| 144 | +**Example Response**: |
| 145 | +```json |
| 146 | +[ |
| 147 | + { |
| 148 | + "id": 1, |
| 149 | + "text": "Buy groceries", |
| 150 | + "completed": false |
| 151 | + }, |
| 152 | + { |
| 153 | + "id": 2, |
| 154 | + "text": "Finish homework", |
| 155 | + "completed": true |
| 156 | + } |
| 157 | +] |
| 158 | +``` |
| 159 | + |
| 160 | +### 2. `create-todo` |
| 161 | +Creates a new todo for the authenticated user. |
| 162 | + |
| 163 | +**Parameters**: |
| 164 | +- `text` (string, required): The text content of the todo |
| 165 | +- `completed` (boolean, optional): Whether the todo is completed (defaults to false) |
| 166 | + |
| 167 | +**Example Response**: |
| 168 | +```json |
| 169 | +{ |
| 170 | + "id": 1234567890, |
| 171 | + "text": "New todo item", |
| 172 | + "completed": false |
| 173 | +} |
| 174 | +``` |
| 175 | + |
| 176 | +### 3. `update-todo` |
| 177 | +Updates an existing todo for the authenticated user. |
| 178 | + |
| 179 | +**Parameters**: |
| 180 | +- `id` (number, required): The ID of the todo to update |
| 181 | +- `text` (string, optional): The new text content of the todo |
| 182 | +- `completed` (boolean, optional): Whether the todo is completed |
| 183 | + |
| 184 | +**Example Response**: |
| 185 | +```json |
| 186 | +{ |
| 187 | + "id": 1234567890, |
| 188 | + "text": "Updated todo text", |
| 189 | + "completed": true |
| 190 | +} |
| 191 | +``` |
| 192 | + |
| 193 | +### 4. `delete-todo` |
| 194 | +Deletes a todo for the authenticated user. |
| 195 | + |
| 196 | +**Parameters**: |
| 197 | +- `id` (number, required): The ID of the todo to delete |
| 198 | + |
| 199 | +**Example Response**: |
| 200 | +```json |
| 201 | +{ |
| 202 | + "success": true, |
| 203 | + "deletedId": 1234567890 |
| 204 | +} |
| 205 | +``` |
| 206 | + |
| 207 | +### 5. `get-clerk-user-data` |
| 208 | +Gets data about the Clerk user that authorized the request. |
| 209 | + |
| 210 | +**Parameters**: None |
| 211 | + |
| 212 | +**Example Response**: |
| 213 | +```json |
| 214 | +{ |
| 215 | + "id": "user_xxxxxxxxxxxxx", |
| 216 | + "firstName": "John", |
| 217 | + "lastName": "Doe", |
| 218 | + "emailAddresses": [...], |
| 219 | + ... |
| 220 | +} |
| 221 | +``` |
| 222 | + |
| 223 | +## Using the MCP Server with Claude |
| 224 | + |
| 225 | +Once configured, you can interact with your todos naturally through Claude: |
| 226 | + |
| 227 | +**Example Prompts**: |
| 228 | +- "Show me all my todos" |
| 229 | +- "Create a new todo to buy milk" |
| 230 | +- "Mark todo #123 as completed" |
| 231 | +- "Delete the todo about buying groceries" |
| 232 | +- "What todos do I have pending?" |
| 233 | + |
| 234 | +Claude will use the appropriate MCP tools to interact with your todo list securely. |
| 235 | + |
| 236 | +## Security |
| 237 | + |
| 238 | +- All MCP requests are authenticated using Clerk OAuth tokens |
| 239 | +- The middleware ensures `.well-known` endpoints are publicly accessible for OAuth discovery |
| 240 | +- All other routes remain protected by Clerk authentication |
| 241 | +- Each user can only access their own todos (enforced by the todoRepository) |
| 242 | + |
| 243 | +## Troubleshooting |
| 244 | + |
| 245 | +### MCP Server Not Connecting |
| 246 | + |
| 247 | +1. Verify the development server is running |
| 248 | +2. Check that dynamic client registration is enabled in Clerk Dashboard |
| 249 | +3. Ensure the URLs in Claude Desktop config match your server URLs |
| 250 | +4. Check browser/Claude console for error messages |
| 251 | + |
| 252 | +### Authentication Errors |
| 253 | + |
| 254 | +1. Verify your Clerk API keys are set correctly in `.env` |
| 255 | +2. Ensure the Clerk Frontend API URL in Claude config is correct |
| 256 | +3. Try re-authenticating by disconnecting and reconnecting the MCP server in Claude |
| 257 | + |
| 258 | +### Tool Execution Errors |
| 259 | + |
| 260 | +1. Check the Next.js server logs for detailed error messages |
| 261 | +2. Verify the database is accessible and properly configured |
| 262 | +3. Ensure the authenticated user has permission to perform the action |
| 263 | + |
| 264 | +## Files Created/Modified |
| 265 | + |
| 266 | +### New Files |
| 267 | +- `src/app/[transport]/route.ts` - Main MCP server handler |
| 268 | +- `src/app/.well-known/oauth-protected-resource/mcp/route.ts` - OAuth resource metadata |
| 269 | +- `src/app/.well-known/oauth-authorization-server/route.ts` - OAuth server metadata |
| 270 | + |
| 271 | +### Modified Files |
| 272 | +- `src/middleware.ts` - Updated to allow public access to `.well-known` endpoints |
| 273 | +- `package.json` - Added MCP dependencies |
| 274 | + |
| 275 | +### Dependencies Added |
| 276 | +- `@vercel/mcp-adapter` - MCP protocol handler |
| 277 | +- `@clerk/mcp-tools` - Clerk OAuth integration for MCP |
| 278 | + |
| 279 | +## Additional Resources |
| 280 | + |
| 281 | +- [Clerk MCP Documentation](https://clerk.com/docs/nextjs/guides/development/mcp/build-mcp-server) |
| 282 | +- [MCP Specification](https://modelcontextprotocol.io/) |
| 283 | +- [Clerk OAuth Documentation](https://clerk.com/docs/guides/development/verifying-oauth-access-tokens) |
| 284 | +- [Vercel MCP Adapter](https://github.com/vercel/mcp-adapter) |
| 285 | +- [Example Repository](https://github.com/clerk/mcp-nextjs-example) |
0 commit comments