This project demonstrates a Model Context Protocol (MCP) Server and Client implementation using TypeScript. The MCP allows AI assistants to securely access external tools, data sources, and services.
Provides three main capabilities:
-
Resources - Data access:
users://all- Get all users from the databaseusers://{userId}/profile- Get specific user details
-
Tools - Actions you can perform:
create-user- Manually create a new usercreate-random-user- Generate a fake user with AI
-
Prompts - Templates for AI interactions:
generate-fake-user- Generate fake user data based on a name
An interactive CLI that connects to the server and allows you to:
- Query: Ask natural language questions using Google's Gemini AI
- Use Tools: Create users manually or with AI
- Access Resources: View user data
- Use Prompts: Generate content with AI assistance
npm install- Get your API key from Google AI Studio
- Add it to the
.envfile:
GEMINI_API_KEY=your_actual_api_key_herenpm run server:buildnpm run server:devnpm run client:devThis provides a web-based interface to test your MCP server:
npm run server:inspectThen open your browser to the provided URL.
When you run the client, you'll get a menu with options:
-
Query: Ask natural language questions like:
- "Show me all users"
- "Create a new user named John Doe"
- "Generate a random user"
-
Tools: Directly use server tools:
- Create users manually by providing name, email, address, phone
- Generate random users with AI
-
Resources: Browse available data:
- View all users
- Get specific user details by ID
-
Prompts: Use AI templates:
- Generate fake user data based on a name
- "Can you show me all the users in the database?"
- "Create a new user with the name Sarah Connor"
- "Generate 3 random users for testing"
- "Show me details for user ID 1"
├── src/
│ ├── server.ts # MCP Server implementation
│ ├── client.ts # Interactive CLI client
│ └── data/
│ └── users.json # User database (JSON file)
├── build/ # Compiled JavaScript files
├── package.json # Dependencies and scripts
├── tsconfig.json # TypeScript configuration
└── .env # Environment variables
npm run server:dev- Run server in development modenpm run server:build- Build TypeScript to JavaScriptnpm run server:build:watch- Build with watch modenpm run server:inspect- Run with MCP Inspectornpm run client:dev- Run the interactive client
The Model Context Protocol (MCP) is an open standard that enables secure connections between AI assistants and external data sources. This project demonstrates:
- Resources: Read-only data access (like databases or APIs)
- Tools: Actions the AI can perform (like creating, updating, deleting)
- Prompts: Templates for AI interactions
- Sampling: Allows the server to request AI completions
- Resource Templates: Dynamic URIs with parameters
- Tool Execution: Server-side actions with validation
- AI Integration: Using Google's Gemini for text generation
- Prompt Templates: Structured AI interactions
- JSON Schema Validation: Type-safe parameter handling
- File System Operations: Reading and writing JSON data
Try these enhancements:
- Add more user fields (avatar, role, created_date)
- Implement user search and filtering
- Add user deletion and update tools
- Connect to a real database instead of JSON
- Add authentication and authorization
- Create more complex prompts and workflows
- "Module not found" errors: Run
npm installandnpm run server:build - API key errors: Make sure your Gemini API key is correctly set in
.env - Connection errors: Ensure the server is running before starting the client
- Permission errors: Check file permissions for
src/data/users.json