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 toolsPOST /mcp/tools/call: Call a tool by name with parametersGET /mcp/tools/schema/<tool_name>: Get the schema for a tool
Resources
GET /mcp/resources/list: List all available resourcesGET /mcp/resources/<resource_id>: Get a resource by IDPOST /mcp/resources/: Create a new resourcePUT /mcp/resources/<resource_id>: Update a resourceDELETE /mcp/resources/<resource_id>: Delete a resourceGET /mcp/resources/<resource_id>/metadata: Get metadata for a resource
Prompts
GET /mcp/prompts/list: List all available prompt templatesGET /mcp/prompts/<prompt_id>: Get a prompt template by IDPOST /mcp/prompts/<prompt_id>/format: Format a prompt template with variablesPOST /mcp/prompts/: Create a new prompt templatePUT /mcp/prompts/<prompt_id>: Update a prompt templateDELETE /mcp/prompts/<prompt_id>: Delete a prompt template
Documentation
GET /mcp/docs: API documentationGET /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.
Metadata
Release files for flaskmcp 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| flaskmcp-0.1.2.tar.gz | 29.3 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| flaskmcp-0.1.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 66.3 kB
Release files / flaskmcp-0.1.2.tar.gz
| Download URL | flaskmcp-0.1.2.tar.gz |
|---|---|
| Size | 29.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6f7f4dce39f43f7b47f82f66a0a0a9894feb30a1af85d9d884f2d49350bc9028
|
|
BLAKE2b-256 checksum How to use checksums |
5559cc5b9c107b8e965c4daa9485cb88bf27c66489c8a2ef1eed15af98269fa1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.10.13
|
Release files / flaskmcp-0.1.2-py3-none-any.whl
| Download URL | flaskmcp-0.1.2-py3-none-any.whl |
|---|---|
| Size | 37.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
625e1266281a882773ebf233c4f84f789eaee47dac6fc2b2f09bdbeece09e649
|
|
BLAKE2b-256 checksum How to use checksums |
1db950d68afbe8057f46729fa329dde057d87138106c7429dc67cafbf6374838
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.10.13
|
Security for flaskmcp 0.1.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.