Skip to main content

A Flask-based implementation of the Model Context Protocol (MCP)

Project description

FlaskMCP

FlaskMCP is a Flask-based implementation of the Model Context Protocol (MCP), designed to standardize communication between language models and context providers.

Features

  • Tool Registry: Register Python functions as tools that can be called through the API
  • Resource Management: Manage static and dynamic resources accessible to models
  • Prompt Templates: Define, manage, and format prompt templates with variables
  • Context Management: Track conversation context between multiple requests
  • API Documentation: Auto-generated API documentation with OpenAPI support
  • Schema Validation: Input and output validation using JSON Schema
  • Comprehensive Error Handling: Detailed error messages and appropriate status codes

Installation

From PyPI

pip install flaskmcp

From Source

git clone https://github.com/Vprashant/flaskmcp.git
cd flaskmcp
pip install -e .

Quick Start

from flask import Flask
from flaskmcp import create_app, tool, register_resource, register_prompt

# Create a Flask app with FlaskMCP
app = create_app({'DEBUG': True})

# Register a tool
@tool(name="add", description="Add two numbers")
def add(a: float, b: float) -> float:
    return a + b

# Register a resource
register_resource(
    "greeting",
    "Welcome to FlaskMCP!",
    "A welcome message"
)

# Register a prompt template
register_prompt(
    "greeting",
    "Hello, $name! Welcome to FlaskMCP.",
    "A personalized greeting"
)

if __name__ == '__main__':
    app.run(host='127.0.0.1', port=5000)

API Endpoints

FlaskMCP exposes the following API endpoints:

Tools

  • GET /mcp/tools/list: List all available tools
  • POST /mcp/tools/call: Call a tool by name with parameters
  • GET /mcp/tools/schema/<tool_name>: Get the schema for a tool

Resources

  • GET /mcp/resources/list: List all available resources
  • GET /mcp/resources/<resource_id>: Get a resource by ID
  • POST /mcp/resources/: Create a new resource
  • PUT /mcp/resources/<resource_id>: Update a resource
  • DELETE /mcp/resources/<resource_id>: Delete a resource
  • GET /mcp/resources/<resource_id>/metadata: Get metadata for a resource

Prompts

  • GET /mcp/prompts/list: List all available prompt templates
  • GET /mcp/prompts/<prompt_id>: Get a prompt template by ID
  • POST /mcp/prompts/<prompt_id>/format: Format a prompt template with variables
  • POST /mcp/prompts/: Create a new prompt template
  • PUT /mcp/prompts/<prompt_id>: Update a prompt template
  • DELETE /mcp/prompts/<prompt_id>: Delete a prompt template

Documentation

  • GET /mcp/docs: API documentation
  • GET /mcp/docs/openapi.json: OpenAPI specification

Examples

Register and Call a Tool

from flaskmcp import tool, create_app

app = create_app()

@tool(name="multiply", description="Multiply two numbers")
def multiply(a: float, b: float) -> float:
    return a * b

if __name__ == '__main__':
    app.run()

To call this tool through the API:

curl -X POST http://localhost:5000/mcp/tools/call \
  -H "Content-Type: application/json" \
  -d '{"name": "multiply", "params": {"a": 5, "b": 3}}'

Managing Resources

from flaskmcp import register_resource, create_app

app = create_app()

# Register a resource
register_resource(
    "user_data",
    {"name": "Alice", "email": "alice@example.com"},
    "User profile data"
)

if __name__ == '__main__':
    app.run()

To access this resource through the API:

curl -X GET http://localhost:5000/mcp/resources/user_data

Working with Prompt Templates

from flaskmcp import register_prompt, create_app

app = create_app()

# Register a prompt template
register_prompt(
    "email_template",
    """
    Subject: $subject
    
    Dear $name,
    
    $content
    
    Best regards,
    $sender
    """,
    "Email template with variables"
)

if __name__ == '__main__':
    app.run()

To format this prompt through the API:

curl -X POST http://localhost:5000/mcp/prompts/email_template/format \
  -H "Content-Type: application/json" \
  -d '{
    "subject": "Meeting Invitation",
    "name": "Bob",
    "content": "I would like to invite you to a meeting tomorrow at 2 PM.",
    "sender": "Alice"
  }'

Context Management

FlaskMCP includes a context management system for tracking conversation state across multiple requests:

from flaskmcp import create_app
from flaskmcp.context.manager import ContextManager

app = create_app()
context_manager = ContextManager()

# Create a new context
context = context_manager.create_context()
context_id = context.id

# Add messages to the context
context_manager.add_message(context_id, "user", "Hello, how are you?")
context_manager.add_message(context_id, "assistant", "I'm doing well, thank you!")

# Get all messages in the context
messages = context_manager.get_context(context_id).get_messages()

Advanced Usage

For more advanced usage examples, check the examples directory in the repository.

Configuration

FlaskMCP can be configured through the Config class:

from flaskmcp import create_app, Config

custom_config = Config()
custom_config.DEBUG = True
custom_config.MCP_PREFIX = "/api/mcp"
custom_config.PORT = 8080

app = create_app(custom_config.__dict__)

License

This project is licensed under the MIT License - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flaskmcp-0.1.1.tar.gz (14.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flaskmcp-0.1.1-py3-none-any.whl (12.3 kB view details)

Uploaded Python 3

File details

Details for the file flaskmcp-0.1.1.tar.gz.

File metadata

  • Download URL: flaskmcp-0.1.1.tar.gz
  • Upload date:
  • Size: 14.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.13

File hashes

Hashes for flaskmcp-0.1.1.tar.gz
Algorithm Hash digest
SHA256 c79525cb25caa242060735950f5e4e031e25b209ada7b2edfadd34667c9f168e
MD5 c6f18d84c5817bd339ab089e31c44b64
BLAKE2b-256 3265c0dbb3b16b84b0c97f60f4726e213c7efcab85aa3210ad29f81da6dd8e75

See more details on using hashes here.

File details

Details for the file flaskmcp-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: flaskmcp-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 12.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.13

File hashes

Hashes for flaskmcp-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 63ce4ce0403588a8f72fc837e696f53d3181716e3b05220844c18f67071e4684
MD5 1b91c232cae6bcce0f9b342cf586e5f8
BLAKE2b-256 66aedd51213200ed57666bbdab4f247e34873100779f43b7c4f477fc68fad299

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page