Integrations
Manages environment variables for API keys and configuration settings through .env files
Provides a web API framework for hosting the MCP server, with endpoints for chat interactions and conversation management
Provides repository hosting for the MCP server code with instructions for cloning and contributing
MCP Server Implementation
A complete Flask-based implementation of Model Context Protocol (MCP) for enhancing Large Language Model capabilities with external tools.
Overview
This repository demonstrates how to build a server that handles Model Context Protocol (MCP), a method for extending LLM capabilities through tool invocation directly in the model's text output. Unlike function calling, MCP places tool definitions directly in the context window and parses the model's natural language responses to identify tool usage.
Features
- 🔧 Complete MCP Implementation: Full parsing, execution, and response handling
- 🌤️ Sample Tools: Weather and calculator tools with parameter validation
- 🔄 Conversation Flow: Maintains context across multiple interactions
- 🧩 Regex-Based Parsing: Flexible text parsing for tool invocations
- 🚀 Flask API: REST API endpoints for chat integration
Project Structure
Installation
- Clone the repository:Copy
- Create a virtual environment:Copy
- Install dependencies:Copy
- Set up environment variables:Copy
Usage
Running the Server
Start the Flask development server:
For production:
API Endpoints
- POST /chat: Process chat messages with MCPCopy
Standalone Example
Run the example script to see MCP in action:
How It Works
- Tool Registration: Tools are registered with their parameters and execution logic
- Tool Definition Injection: XML-formatted tool descriptions are added to the prompt
- LLM Response Processing: Regex patterns identify tool calls in the LLM's text output
- Tool Execution: Parameters are parsed and passed to appropriate tool handlers
- Result Injection: Tool execution results are inserted back into the response
MCP vs. Function Calling
Feature | MCP | Function Calling |
---|---|---|
Definition Location | In prompt text | In API parameters |
Invocation Format | Natural language | Structured JSON |
Implementation | Text parsing | API integration |
Visibility | Visible in response | May be hidden |
Platform Support | Any text-based LLM | Requires API support |
Example Conversation
User: What's the weather like in Boston?
LLM:
After Processing:
Adding Your Own Tools
- Create a new class inheriting from
Tool
- Define parameters and execution logic
- Register with the MCP handler
Example:
MCP Configuration and Invocation Flow
- Tool Registration:
- MCP tools are registered with the handler
- Each tool provides its name, description, and parameter definitions
- Tool Definition Injection:
- Tool definitions are added to the system message
- Format follows XML structure for MCP
- LLM Response Processing:
- LLM generates responses that may include tool invocations
- Pattern matching identifies tool calls in the text
- Tool parameters are parsed and passed to tool execution methods
- Tool Execution:
- Tools are executed with the provided parameters
- Results are injected back into the conversation
- Conversation Management:
- Processed responses with tool results are added to conversation history
- Future LLM requests include this history for context
Example Conversation
Here's an example of what a conversation might look like:
User: What's the weather like in Boston?
System: Sends prompt with MCP tool definitions to LLM
LLM Response:
MCP Handler: Parses the response, finds the tool call, and executes the weather tool
Tool Execution Result:
Processed Response (sent back to user):
User: Can you calculate the square root of 144?
LLM Response:
MCP Handler: Parses response, executes calculator tool
Tool Execution Result:
Processed Response (sent back to user):
This demonstrates the complete flow of MCP tool usage, from the LLM's text-based invocation through execution and response processing.
License
MIT
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
This server cannot be installed
A Flask-based server that implements Model Context Protocol to enhance LLMs with external tool capabilities via natural language, allowing tools like weather lookup and calculations to be invoked directly in the model's text output.