Last active
          June 21, 2025 16:27 
        
      - 
      
- 
        Save Njengah/2a3deb2ccaf56bcde5b55d60b9a2dbe0 to your computer and use it in GitHub Desktop. 
    Building Your First MCP Server - building a task management MCP server that connects AI to a simple to-do system.
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | import { Server } from '@modelcontextprotocol/sdk/server/index.js'; | |
| import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'; | |
| import fs from 'fs/promises'; | |
| class TaskMCPServer { | |
| constructor() { | |
| this.server = new Server( | |
| { name: 'task-server', version: '1.0.0' }, | |
| { capabilities: { tools: {} } } | |
| ); | |
| this.setupTools(); | |
| this.setupTransport(); | |
| } | |
| setupTools() { | |
| // Register available tools | |
| this.server.setRequestHandler('tools/list', async () => ({ | |
| tools: [ | |
| { | |
| name: 'add_task', | |
| description: 'Add a new task to the list', | |
| inputSchema: { | |
| type: 'object', | |
| properties: { | |
| title: { type: 'string' }, | |
| priority: { type: 'string', enum: ['low', 'medium', 'high'] } | |
| }, | |
| required: ['title'] | |
| } | |
| }, | |
| { | |
| name: 'get_tasks', | |
| description: 'Retrieve all tasks', | |
| inputSchema: { type: 'object', properties: {} } | |
| } | |
| ] | |
| })); | |
| // Handle tool execution | |
| this.server.setRequestHandler('tools/call', async (request) => { | |
| const { name, arguments: args } = request.params; | |
| switch (name) { | |
| case 'add_task': | |
| return await this.addTask(args.title, args.priority || 'medium'); | |
| case 'get_tasks': | |
| return await this.getTasks(); | |
| default: | |
| throw new Error(`Unknown tool: ${name}`); | |
| } | |
| }); | |
| } | |
| async addTask(title, priority) { | |
| const tasks = await this.loadTasks(); | |
| const newTask = { | |
| id: Date.now(), | |
| title, | |
| priority, | |
| completed: false, | |
| created: new Date().toISOString() | |
| }; | |
| tasks.push(newTask); | |
| await this.saveTasks(tasks); | |
| return { | |
| content: [{ | |
| type: 'text', | |
| text: `Task "${title}" added successfully with ${priority} priority` | |
| }] | |
| }; | |
| } | |
| async getTasks() { | |
| const tasks = await this.loadTasks(); | |
| return { | |
| content: [{ | |
| type: 'text', | |
| text: JSON.stringify(tasks, null, 2) | |
| }] | |
| }; | |
| } | |
| async loadTasks() { | |
| try { | |
| const data = await fs.readFile('tasks.json', 'utf8'); | |
| return JSON.parse(data); | |
| } catch { | |
| return []; | |
| } | |
| } | |
| async saveTasks(tasks) { | |
| await fs.writeFile('tasks.json', JSON.stringify(tasks, null, 2)); | |
| } | |
| setupTransport() { | |
| const transport = new StdioServerTransport(); | |
| this.server.connect(transport); | |
| } | |
| } | |
| new TaskMCPServer(); | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment