A comprehensive demonstration of the Model Context Protocol (MCP) implemented in Next.js with TypeScript, featuring both HTTP and real-time WebSocket communication, authentication, and a modern web interface.
- 9 MCP tools: Echo, time, weather, filesystem operations, web search, database queries
- 7 MCP resources: Documentation, API specs, configuration files
- Full MCP protocol support: Tools, resources, and proper error handling
- TypeScript integration: Complete type safety throughout
- HTTP MCP client: Traditional request/response pattern
- Real-time WebSocket client: Live communication with instant updates
- Tab-based interface: Easy switching between communication modes
- JWT-based authentication: Secure user sessions
- Role-based access control: Admin and user roles
- HTTP-only cookies: Secure token storage
- Protected routes: Authentication middleware
- Live tool execution: Real-time status tracking
- Instant resource access: Immediate resource reading
- Connection monitoring: WebSocket connection status
- Automatic reconnection: Resilient connection handling
- Execution history: Timestamped operation logs
- SQLite integration: Local database with sample data
- Safe file operations: Restricted filesystem access
- Mock external APIs: Weather and web search services
- Framework: Next.js 15.5.3 with App Router
- Language: TypeScript
- Styling: Tailwind CSS
- Database: SQLite with better-sqlite3
- Authentication: JWT with bcryptjs
- WebSocket: ws library
- MCP SDK: @modelcontextprotocol/sdk
-
Clone the repository:
git clone <repository-url> cd mcp-demo
-
Install dependencies:
npm install
-
Start the development server:
npm run dev
-
Open your browser: Navigate to http://localhost:3000
- Register/Login: Create an account or use existing credentials
- Choose interface: Switch between HTTP and WebSocket clients
- Execute tools: Try different MCP tools with real-time feedback
- Read resources: Access documentation and configuration files
- echo: Echo back messages
- get_time: Get current timestamp
- get_weather: Mock weather information
- list_files: Browse project files safely
- read_file: Read file contents with size limits
- web_search: Mock web search results
- db_query: Execute SQL SELECT queries
- db_schema: View database schema
- db_samples: Get sample database queries
- MCP overview: Introduction to Model Context Protocol
- Tool development guide: How to create MCP tools
- Best practices: Security and performance guidelines
- API documentation: Weather and database endpoints
- Configuration: Server settings and tool registry
src/
βββ app/ # Next.js App Router
β βββ api/ # API routes
β β βββ auth/ # Authentication endpoints
β β βββ mcp/ # MCP HTTP endpoints
β β βββ ws/ # WebSocket stats
β βββ layout.tsx # Root layout with auth provider
β βββ page.tsx # Main application page
βββ components/ # React components
β βββ AuthForm.tsx # Login/register forms
β βββ RealtimeMCPClient.tsx # WebSocket client
βββ contexts/ # React contexts
β βββ AuthContext.tsx # Authentication state
βββ hooks/ # Custom React hooks
β βββ useWebSocket.ts # WebSocket connection hook
βββ lib/ # Utility libraries
β βββ auth.ts # Authentication utilities
β βββ database.ts # Database setup
β βββ middleware.ts # Route protection
β βββ websocket-server.ts # WebSocket server
βββ mcp-server/ # MCP implementation
βββ tools/ # MCP tools
βββ resources/ # MCP resources
βββ server.ts # MCP server
βββ types.ts # TypeScript definitions
server.ts
: Custom Next.js server with WebSocket supportsrc/lib/websocket-server.ts
: WebSocket server implementationsrc/hooks/useWebSocket.ts
: React WebSocket hooksrc/mcp-server/
: Complete MCP server implementation
- Create a new tool file in
src/mcp-server/tools/
- Implement the tool with proper input validation
- Register the tool in the tools index
- The tool will automatically appear in both HTTP and WebSocket clients
- Add resource definition in
src/mcp-server/resources/index.ts
- Implement content provider in
src/mcp-server/resources/content.ts
- Resources will be available in both communication modes
- Input validation: All tool inputs are validated
- Path security: Filesystem operations are restricted to project directory
- SQL injection protection: Database queries are limited to SELECT statements
- Authentication required: All MCP operations require valid authentication
- Role-based access: Different permissions for admin and user roles
POST /api/auth/register
- User registrationPOST /api/auth/login
- User loginPOST /api/auth/logout
- User logoutGET /api/auth/me
- Get current user
GET /api/mcp
- Server statusPOST /api/mcp
- MCP method calls (tools/list, resources/list, etc.)
ws://localhost:3000/ws/mcp
- WebSocket MCP connectionGET /api/ws/stats
- WebSocket connection statistics
# Standard Next.js development (Vercel-compatible)
npm run dev
# Full development with WebSocket support
npm run dev:full
npm run build
npm start
-
Connect to Vercel:
npx vercel
-
Set environment variables in Vercel dashboard:
JWT_SECRET
: Secret key for JWT tokensNODE_ENV
: Set toproduction
-
Deploy:
npx vercel --prod
JWT_SECRET
: Secret key for JWT tokens (default: development key)PORT
: Server port (default: 3000)NODE_ENV
: Environment mode
This demo covers:
- MCP protocol: Complete implementation of Model Context Protocol
- Tool development: Creating and registering MCP tools
- Resource management: Handling MCP resources and content
- Authentication: JWT-based auth with role-based access
- WebSocket integration: Real-time communication patterns
- Database operations: SQLite integration with MCP
- Security best practices: Input validation and access control
- Modern React patterns: Hooks, contexts, and TypeScript
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
- Model Context Protocol for the MCP specification
- Next.js for the excellent React framework
- Tailwind CSS for the utility-first CSS framework