MCP Servers in GitHub Copilot - neerajk555/Github-Copilot-Workshop GitHub Wiki

Building and Understanding an MCP Server (Model Context Protocol) with GitHub Copilot

This guide walks through how to build a custom MCP server using Python and integrate it into GitHub Copilot Chat via Agent Mode.


πŸ” What is an MCP Server?

MCP (Model Context Protocol) allows you to extend LLMs like GitHub Copilot by plugging in your own tools (functions, APIs, etc.) that the model can call during a conversation.


πŸ”§ Why MCP?

MCP servers let LLMs:

  • Run custom functions
  • Query live data from external APIs
  • Call your internal business logic
  • Fetch structured resources
  • Use prompt templates dynamically

πŸ” Architecture Overview

Component Role
MCP Server (Python) Implements tools, resources, prompts
Copilot Chat (Agent) Discovers and calls those tools during a chat
MCP Protocol JSON-RPC over stdio or SSE

🧠 Step-by-Step: What’s Happening

1. Define Tools in Python

@mcp.tool()
def add(a: int, b: int) -> int:
    return a + b

2. Define Resources

@mcp.resource("weather://{city}")
def get_weather(city: str) -> str:
    return f"{city} is sunny today."

3. Define Prompt Templates

@mcp.prompt()
def greet(name: str) -> str:
    return f"Write a friendly message for {name}."

▢️ Run Your Server

python server.py

πŸ”Œ Connect to Copilot Chat (Agent Mode)

Create .vscode/mcp.json:

{
  "servers": {
    "custom-mcp": {
      "command": "python",
      "type": "stdio",
      "args": ["${workspaceFolder}/server.py"]
    }
  }
}

πŸ’¬ Sample Interactions

  • Ask: What's 7 + 3? β†’ Copilot uses add(a, b) and returns 10
  • Ask: weather://London β†’ Copilot calls get_weather("London")

πŸ” Security Notes

  • Secure API keys
  • Don’t expose unsafe logic
  • MCP tools can execute code

πŸ§ͺ Bonus: Under the Hood (JSON-RPC)

Request:

{
  "jsonrpc": "2.0",
  "method": "add",
  "params": { "a": 7, "b": 3 },
  "id": 1
}

Response:

{
  "jsonrpc": "2.0",
  "result": 10,
  "id": 1
}

βœ… Summary

  • Build tools/resources/prompts
  • Expose them via @mcp.* decorators
  • Connect them via .vscode/mcp.json
  • Use in Copilot Agent Mode

πŸ”§ Want to Build More?

You can extend this with:

  • Vector search tool
  • File explorer
  • Codebase search
  • Slack integration
  • Your internal APIs