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 usesadd(a, b)
and returns10
- Ask:
weather://London
β Copilot callsget_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