Skip to content

codenjoyme/mcpyrex-python

Repository files navigation

Extending GitHub Copilot with Python and Langchain via MCP

This project demonstrates how to extend GitHub Copilot capabilities using Python, Langchain, and the Model Context Protocol (MCP), allowing for more deterministic and powerful interactions with the LLM.

Project Overview

As a GitHub Copilot trainer, I've observed its impressive evolution. However, I've always wanted to access GitHub Copilot's internals to build more complex transformation chains. This project shows how to achieve that goal by leveraging MCP (Model Context Protocol) and Langchain.

While GitHub Copilot repository custom instructions feature improved customization options, the introduction of the MCP protocol opened new possibilities for extending Copilot's functionality through custom MCP servers.

The Problem

Instruction files are not always deterministic - they need to be fine-tuned when new LLM versions are released to reduce hallucinations. LLMs often struggle with precise text manipulations, sometimes creatively reinterpreting tasks and adding unwanted artifacts. What we need is a way to inject deterministic logic into our instructions.

The Solution

This project demonstrates a solution through:

  1. Python scripts installed on the local machine.
  2. Custom MCP server configuration through .vscode/mcp.json
  3. Custom tools defined in the mcp_server/tools directory

With this setup, GitHub Copilot gains access to new, well-documented tools that it can see as part of your project. When you ask Copilot to "create a tool that does X", it can generate a solution very close to what you need. You simply accept its changes and restart MCP to get a new deterministic tool for your specific logic.

Data Security

⚠️ Important: Please review the Security Disclaimer before using this tool in production environments.

This document outlines potential data leak scenarios and provides guidance on risk mitigation when working with this tool.

Getting Started

Run this command in the root folder of your project and follow instructions:

One-line installation (copy-paste friendly):

Windows

With git

git clone https://github.com/codenjoyme/mcpyrex-python.git mcp_server; cd mcp_server\build; .\install-windows.ps1

Without git

$work = "mcp_server"; $url = "https://github.com/codenjoyme/mcpyrex-python/archive/refs/heads/main.zip"; New-Item -ItemType Directory -Force -Path $work; (New-Object System.Net.WebClient).DownloadFile($url, "$work\project.zip"); Expand-Archive -Path "$work\project.zip" -DestinationPath "$work\tmp"; Remove-Item "$work\project.zip"; Move-Item "$work\tmp\mcpyrex-python-main\*" "$work"; Move-Item "$work\tmp\mcpyrex-python-main\.*" "$work" -Force; Remove-Item "$work\tmp" -Recurse; Set-Location "$work\build"; .\install-windows.ps1

Update existing installation:

cd .\mcp_server\build; .\install-windows.ps1

Security policy issues If you got this error:

.\install-windows.ps1 : File C:\workspace\mcp_server\build\install-windows.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https://go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:510
+ ... work\tmp" -Recurse; Set-Location "$work\build"; .\install-windows.ps1
    + CategoryInfo          : SecurityError: (:) [], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess

Please run before script:

Get-ExecutionPolicy Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned

macOS

With git

git clone https://github.com/codenjoyme/mcpyrex-python.git mcp_server && cd mcp_server/build && ./install-macos.sh

Without git

work="mcp_server"; url="https://github.com/codenjoyme/mcpyrex-python/archive/refs/heads/main.zip"; mkdir -p "$work"; curl -L -o "$work/project.zip" "$url"; cd "$work"; unzip -q project.zip; mv mcpyrex-python-main/* .; mv mcpyrex-python-main/.* . 2>/dev/null || true; rmdir mcpyrex-python-main; rm project.zip; cd build; chmod +x ./install-macos.sh; ./install-macos.sh

Update existing installation:

cd ./mcp_server/build && ./install-macos.sh

Linux

With git

git clone https://github.com/codenjoyme/mcpyrex-python.git mcp_server && cd mcp_server/build && ./install-linux.sh

Without git

work="mcp_server"; url="https://github.com/codenjoyme/mcpyrex-python/archive/refs/heads/main.zip"; mkdir -p "$work"; wget -O "$work/project.zip" "$url"; cd "$work"; unzip -q project.zip; mv mcpyrex-python-main/* .; mv mcpyrex-python-main/.* . 2>/dev/null || true; rmdir mcpyrex-python-main; rm project.zip; cd build; ./install-linux.sh

Update existing installation:

cd ./mcp_server/build && ./install-linux.sh

Installation process

  1. This script will:

    • Download the latest version from GitHub to ./.mcp-python folder
    • Extract all files
    • Navigate to the build directory
    • Run the interactive installation script automatically
  2. The installation script will automatically:

    • Detect or install Python 3.11+
    • Create virtual environment
    • Ask you to choose between Cursor or VSCode
    • Copy appropriate configuration files
    • Install Python dependencies via pip
    • Set up your MCP server integration
  3. After installation:

    • Your .vscode/mcp.json or .cursor/mcp.json will be configured
    • Your workspace settings will be updated
    • GitHub Copilot instructions will be in place
    • Start using enhanced GitHub Copilot capabilities!

Key Components

  • Simple Examples (mcp_server/simple/): Demonstrates integration with different LLM providers and key LangChain concepts

    • query_openai.py - OpenAI integration
    • query_azure.py - Azure OpenAI integration
    • prompt_template.py - Prompt template management and usage
    • rag.py - Retrieval-Augmented Generation (RAG) with vector search
    • agent.py - AI Agent with specialized tools (character counting, MD5 hashing, regex matching)
    • structured_output.py - Structured data extraction in multiple formats (JSON, YAML, Markdown)
    • chain_of_thought.py - Step-by-step problem solving with transparent reasoning
    • color.py - Helper module for colored terminal output
  • MCP Implementation:

    • mcp_server/test/server.py - Client implementation for testing
    • mcp_server/ - Server with custom tools
  • Install file mcp_server/build/install.ps1:

    • Setup virtual environment
    • Setup libraries
    • Smoke tests

mcp.json

{
  "servers": {
    "mcpyrex": {
        "type": "stdio",
        "command": "${workspaceFolder}\\.mcp-python\\langchain_env\\Scripts\\python.exe",
        "args": ["${workspaceFolder}\\.mcp-python\\mcp_server\\server.py"]
    }
  }
}

Custom Langchain Tools

  • lng_batch_run - advanced pipeline execution with conditionals, loops, and parallel processing
  • lng_count_words - word counting, demonstrates python function calling
  • lng_get_tools_info - tools information retrieval, collects all the information about tools in one place, that helps in Github Copilot.
  • lng_llm_rag_add_data and lng_rag_search - demonstrates RAG (Retrieval Augmented Generation) functionality
  • lng_llm_prompt_template - unified prompt template management with file storage (save, use, list)
  • lng_llm_run_chain - demostrates Chain execution
  • lng_llm_agent_demo - demonstrates Agent functionality
  • lng_llm_structured_output - demonstrates Structured output
  • lng_llm_chain_of_thought - demonstrates Chain of Thought reasoning approach with Memory usage
  • And more in mcp_server/tools/

Running tools in terminal

You can run tools directly in the terminal using the mcp_server/run.py script. This allows you to quickly test and execute any tool without the overhead of the MCP server.

python -m mcp_server.run
python -m mcp_server.run list
python -m mcp_server.run schema lng_count_words
python -m mcp_server.run run lng_count_words '{\"input_text\":\"Hello world\"}'
python -m mcp_server.run run lng_math_calculator '{\"expression\":\"2+3*4\"}'
python -m mcp_server.run batch lng_count_words '{\"input_text\":\"Hello\"}' lng_math_calculator '{\"expression\":\"2+3\"}'

Debug

To debug mcp protocol you can run server in terminal .\langchain_env\Scripts\python.exe .\mcp_server\server.py, then update mcp.json from "args": ["${workspaceFolder}\\mcp_server\\server.py"] to "args": ["${workspaceFolder}\\mcp_server\\server_fake.py"] and run fake server. After that copy request from mcp_server/logs/mcp_out.log:

2025-07-16 20:54:30,339 - mcp_fake_logger - INFO - [<] {"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-03-26","capabilities":{"roots":{"listChanged":true}},"clientInfo":{"name":"Visual Studio Code - Insiders","version":"1.100.0-insider"}}}

to the server console. Then copy output to the mcp_server/logs/mcp_in.log and save. Then reiterate.

Use Cases

While LLMs can handle simple tasks, more complex operations like processing Excel files or extracting specific data benefit greatly from custom MCP tools. The LLM can still help generate the code for these tools, providing the best of both worlds.

MCP Configuration

Enabling/Disabling MCP in VS Code

To control whether MCP (Model Context Protocol) is enabled or disabled, you need to modify the ${workspaceFolder}/.vscode/settings.json file:

To Enable MCP:

{
    "chat.mcp.enabled": true,
    "github.copilot.chat.codeGeneration.useInstructionFiles": false
}

To Disable MCP:

{
    "chat.mcp.enabled": false,
    "github.copilot.chat.codeGeneration.useInstructionFiles": true
}

API Keys

API keys and other credentials should be stored in a .env file (not included in the repository).

Original Source

This project is based on concepts from the blog post: Как расширить GithubCopilot с помощью Python и Langchain через MCP

Repository

The original repository is available at: https://github.com/codenjoyme/mcpyrex-python

Alternative HTTP Proxy Setup (When MCP is Not Available)

If you're experiencing issues with MCP connectivity or want to test the system without full MCP integration, this project provides an alternative HTTP-based architecture that allows you to quickly test and interact with LangChain tools.

Architecture Overview

The alternative setup consists of three components:

  1. Component 1: mcp_server/server.py - The original MCP server with LangChain tools (will run automatically with Component 2)
  2. Component 2: mcp_server/proxy.py - Full HTTP proxy server with complete MCP protocol implementation
  3. Component 3: mcp_server/execute.py - Client for quick requests

Quick Start

1. Start the MCP HTTP Proxy Server

Open a terminal and run:

python mcp_server/proxy.py

The server will start on http://127.0.0.1:8080 and provide:

  • GET /health - Server health check with MCP connection status
  • GET /tools - List all available LangChain tools
  • POST /execute - Execute tools using full MCP protocol

2. Test the System

Open another terminal and test the system:

Check server health:

python mcp_server/execute.py health

List available tools:

python mcp_server/execute.py list

Execute tools:

# Count words in text
python mcp_server/execute.py exec lng_count_words --params '{\"input_text\": \"Hello world this is a test\"}'

# Math calculations
python mcp_server/execute.py exec lng_math_calculator --params '{\"expression\": \"2 + 3 * 4\"}'

# Chain of thought reasoning
python mcp_server/execute.py exec lng_chain_of_thought --params '{\"question\": \"What is 15 * 24?\"}'

# RAG functionality
python mcp_server/execute.py exec lng_rag_add_data --params '{\"input_text\": \"Your document content\"}'
python mcp_server/execute.py exec lng_rag_search --params '{\"query\": \"search term\"}'

3. View Available Examples

python mcp_server/execute.py examples

Troubleshooting

If you encounter issues:

  1. Server not starting: Check if port 8080 is available
  2. Connection refused: Ensure the test server is running
  3. JSON parsing errors: Use single quotes for PowerShell parameters
  4. Tool errors: Check the server console for detailed error messages

Browser Testing

You can also test the system directly in your browser:

  • Health check: http://127.0.0.1:8080/health
  • Use tools like Postman or curl for POST requests to /execute

This alternative setup provides a reliable fallback when MCP is not available while maintaining full access to your LangChain tools.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors