Understanding MCP (Model Context Protocol) for AI Agents and its practical use

Table of contents

๐Ÿง  What is MCP?

  • MCP stands for Model Context Protocol.

  • It is a standard protocol that enables AI agents to interact with software applications much like how software engineers use REST APIs.

๐Ÿ“Œ Why MCP Was Needed:

  • Just like software engineers use REST clients + HTTP protocol to interact with REST API servers (e.g., GitHub, Jira), AI agents need a standardized and secure method to perform similar tasks.

  • MCP provides context-awareness and security, which traditional LLM-based agents often lack.

๐Ÿงฑ MCP Workflow (Parallels with REST):

Software EngineersAI Agents (MCP)
REST ClientMCP Client
HTTP ProtocolMCP Protocol
REST API ServerMCP Server (built by apps)

Applications (like GitHub, Jira, AWS) implement their own MCP servers.

AI agents use MCP clients to convert user prompts into MCP format and communicate with these servers.

๐Ÿ› ๏ธ Demonstration:

  • Using Visual Studio Code and GitHub Copilot Chat (as MCP client).

  • Showed how to:

    • Configure VS Code with GitHub MCP server JSON.

    • Use prompts to create a GitHub repository via MCP.

    • Provide GitHub API token securely.

    • Interact with other tasks like pull requests, issues, file contents.

Step by step implementation - Demonstration with VS code

  1. Ensure VS Code Version: Make sure you are using Visual Studio Code version 1.99 or later, as MCP client functionality is supported from this version onwards. Update if necessary via Code > Check for Updates.

  2. Open User Settings (JSON): Press Ctrl+Shift+P (Windows/Linux) or Cmd+Shift+P (Mac) and search for "Open User Settings (JSON)".

     {
         "mcp": {
           "inputs": [
             {
               "type": "promptString",
               "id": "github_token",
               "description": "GitHub Personal Access Token",
               "password": true
             }
           ],
           "servers": {
             "github": {
               "command": "docker",
               "args": [
                 "run",
                 "-i",
                 "--rm",
                 "-e",
                 "GITHUB_PERSONAL_ACCESS_TOKEN",
                 "ghcr.io/github/github-mcp-server"
               ],
               "env": {
                 "GITHUB_PERSONAL_ACCESS_TOKEN": "${input:github_token}"
               }
             }
           }
         }
       }
    
  3. Configure GitHub MCP Server in Settings: Paste the JSON configuration for the GitHub MCP server into your settings.json file. This configuration, typically found in the GitHub MCP server's documentation, tells VS Code how to connect to the server (often using a Docker image).

  4. Obtain GitHub API Token:

    • Go to your GitHub account settings.

    • Navigate to "Developer settings" -> "Personal access tokens" -> "Tokens (classic)".

    • Click "Generate new token" -> "Generate new token (classic)".

    • Grant the necessary permissions for the actions you want the AI agent to perform (be cautious and grant only what's needed).

    • Copy the generated token.

  5. Start MCP Server (if not running): The JSON configuration in your settings likely uses Docker to run the MCP server. Ensure Docker is running. VS Code might prompt you to start the server the first time you try to use the MCP client.

  6. Open GitHub Copilot Chat: Look for the chat icon in your VS Code activity bar and click it to open the GitHub Copilot Chat.

  7. Enable Agent Mode: In the chat input area, click on the "Ask" dropdown and select "Agent". This puts Copilot Chat into a mode where it can utilize the configured tools (your MCP server).

  8. Access Tools: Once in agent mode, you should see a "Tools" button in the chat interface. Clicking this will show you the available commands provided by the GitHub MCP server.

  9. Provide a Prompt: In the chat input, type a prompt describing the action you want to perform. For example: create repository name <your_repository_name>.

  10. Confirm and Execute: Copilot Chat (the MCP client) will likely ask for confirmation or additional details. Once you confirm, it will use the MCP protocol to send the request to the GitHub MCP server (whose address was configured in settings.json). You might be prompted to enter your GitHub API token the first time.

    curl -H "Authorization: token YOUR_GITHUB_TOKEN" \
         -H "Accept: application/vnd.github.v3+json" \
         https://api.github.com/user/repos \
         -d '{"name": "mcp-demo-repo"}'
    

    powershell command for windows

    Invoke-RestMethod -Uri "https://api.github.com/user/repos" -Method Post -Headers @{"Authorization" = "token YOUR_GITHUB_TOKEN"; "Accept" = "application/vnd.github.v3+json"} -Body '{"name": "mcp-demo-repo"}'
    
  11. Verify the Action: Check your GitHub account to see if the repository (or other action) was successfully created.

  12. Optional) Stop MCP Server: For security, you can stop the MCP server in VS Code when you are finished using it. You might also want to restart it if you update your API token.

๐Ÿ“ When Should You Develop Your Own MCP Server?

  • Only when dealing with internal tools or custom organizational applications.

  • Otherwise, most popular platforms will provide their own MCP servers.

Conclusion

MCP makes AI agent interactions with applications standardized, secure, and context-rich, similar to how REST APIs revolutionized software engineering automation.

0
Subscribe to my newsletter

Read articles from Abhijit Shenolikar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Abhijit Shenolikar
Abhijit Shenolikar