Viyv MCP
A simple wrapper library for FastMCP + Starlette
Installation
npx viyv-mcpAsk AI about Viyv MCP
Powered by Claude ยท Grounded in docs
I know everything about Viyv MCP. Ask me about installation, configuration, usage, or troubleshooting.
0/500
Reviews
Documentation
viyv_mcp
viyv_mcp is a production-ready Python wrapper around FastMCP and Starlette that simplifies creating MCP (Model Context Protocol) servers with minimal boilerplate.
๐ Quick Start
# Install the package
pip install viyv_mcp
# Create a new MCP server project
create-viyv-mcp new my_mcp_server
# Navigate to the project and install dependencies
cd my_mcp_server
uv sync
# Run the server
uv run python main.py
Your MCP server is now running at http://localhost:8000 ๐
โจ Key Features
๐ ๏ธ Simple Tool Creation
from viyv_mcp import tool
@tool(description="Add two numbers")
def add(a: int, b: int) -> int:
return a + b
๐ External MCP Server Bridge
// app/mcp_server_configs/filesystem.json
{
"command": "npx",
"args": ["@modelcontextprotocol/server-filesystem", "/workspace"],
"env": {
"API_KEY": "$API_KEY" // Environment variable interpolation
},
"cwd": "/path/to/working/dir", // Optional
"tags": ["filesystem", "io"] // Optional: for filtering
}
๐ Production-Ready Multi-Worker Support (New in v0.1.10)
# Enable stateless HTTP mode for multi-worker deployments
STATELESS_HTTP=true uv run python main.py
# Deploy with Gunicorn (recommended for production)
uv pip install gunicorn
STATELESS_HTTP=true uv run gunicorn main:app \
-w 4 -k uvicorn.workers.UvicornWorker -b 0.0.0.0:8000
๐ JWT Authentication & Access Control
# Tools with security metadata
@tool(
description="Query salary",
namespace="hr", # Visible only to hr agents
security_level="confidential", # Requires confidential+ clearance
)
def query_salary(employee_id: str) -> str:
return f"Salary: $100,000"
# Generate JWT for an agent
python -m viyv_mcp generate-jwt \
--sub hr-agent --clearance confidential --namespace hr \
--trust common --expires 24h --secret "$SECRET"
- Namespace: Controls tool visibility (
tools/listfiltering) - Security Level: Controls tool executability (
tools/callclearance check) - Modes: bypass (dev), authenticated (JWT), deny_all (default safe)
- Both transports: Works for stdio (
.mcp.json) and HTTP
๐ Built-in Integrations
- Custom FastAPI Endpoints: Mount additional APIs with
@entry
๐ฆ Installation
# Basic installation
pip install viyv_mcp
# With optional security config (YAML support)
pip install "viyv_mcp[security]"
๐ Project Structure
When you create a new project with create-viyv-mcp new my_project:
my_project/
โโโ main.py # Server entry point
โโโ pyproject.toml # Dependencies (managed by uv)
โโโ Dockerfile # Production-ready container
โโโ .env # Environment variables
โโโ app/
โโโ config.py # Configuration management
โโโ tools/ # MCP tools (@tool decorator)
โโโ resources/ # MCP resources (@resource decorator)
โโโ prompts/ # MCP prompts (@prompt decorator)
โโโ agents/ # AI agents (@agent decorator)
โโโ entries/ # Custom HTTP endpoints (@entry decorator)
โโโ mcp_server_configs/ # External MCP server configurations
๐ป Advanced Usage Examples
Creating Resources with URI Templates
from viyv_mcp import resource
def register(mcp):
@resource("database://{table}/{id}")
def get_record(table: str, id: str) -> dict:
"""Fetch a database record by table and ID"""
# Your database logic here
return {"table": table, "id": id, "data": "..."}
Prompts with Parameters
from viyv_mcp import prompt
from typing import Annotated
from pydantic import Field
def register(mcp):
@prompt("code_review")
def code_review_prompt(
language: Annotated[str, Field(description="Programming language")],
code: Annotated[str, Field(description="Code to review")]
) -> str:
return f"""
Please review this {language} code:
```{language}
{code}
```
Focus on: performance, security, and best practices.
"""
๐ Tool Grouping (New in v0.1.13)
Organize tools into groups for better discoverability and UI presentation:
from viyv_mcp import tool
def register(mcp):
@tool(
description="Add two numbers",
group="่จ็ฎใใผใซ", # Group name
title="ๅ ็ฎ" # UI display name (optional)
)
def add(a: int, b: int) -> int:
return a + b
@tool(
description="Subtract two numbers",
group="่จ็ฎใใผใซ" # Same group
)
def subtract(a: int, b: int) -> int:
return a - b
@tool(
description="Delete a file",
group="ใใกใคใซใทในใใ ",
destructive=True # Destructive operation hint
)
def delete_file(path: str) -> bool:
import os
os.remove(path)
return True
External MCP Server Grouping:
// app/mcp_server_configs/filesystem.json
{
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-filesystem", "/workspace"],
"group": "ใใกใคใซใทในใใ ", // Apply to all tools
"group_map": { // Override per tool (optional)
"read_file": "ใใกใคใซๆไฝ/่ชญใฟ่พผใฟ",
"write_file": "ใใกใคใซๆไฝ/ๆธใ่พผใฟ"
}
}
How it works:
- Group information is stored in
_meta.viyv.group(vendor namespace) - MCP clients can use groups for organized display
- Backward compatible: tools without groups work normally
๐ง Configuration
Environment Variables
# Server Configuration
HOST=0.0.0.0 # Server host (default: 127.0.0.1)
PORT=8000 # Server port (default: 8000)
STATELESS_HTTP=true # Enable stateless mode for multi-worker
# Directory Configuration
BRIDGE_CONFIG_DIR=app/mcp_server_configs # External MCP configs
STATIC_DIR=static/images # Static file serving
Configuration Class
# app/config.py
from viyv_mcp.app.config import Config
class MyConfig(Config):
# Inherit base configuration
@staticmethod
def get_stateless_http():
"""Get stateless HTTP setting from environment"""
env_val = os.getenv("STATELESS_HTTP", "").lower()
if env_val in ("true", "1", "yes", "on"):
return True
elif env_val in ("false", "0", "no", "off"):
return False
return None # Use FastMCP default
๐๏ธ Architecture & Advanced Features
ASGI-Level Routing (SSE Streaming Fix)
viyv_mcp implements custom ASGI routing to fix SSE streaming issues:
- Direct
/mcppath routing bypasses Starlette middleware - Ensures proper Server-Sent Events handling
- Compatible with FastMCP's streaming protocol
Dynamic Tool Injection
- Tools are refreshed on every request
- Agents always have access to the latest tools
- Supports runtime tool filtering with tags
External MCP Server Management
- Child process management with stdio communication
- Automatic tool/resource/prompt registration
- Environment variable interpolation in configs
- Tag-based filtering for selective tool inclusion
Production Deployment Features
Stateless HTTP Mode
- No session ID requirements
- Perfect for load-balanced environments
- Enable with
STATELESS_HTTP=true
Multi-Worker Deployment
# test_app.py - Create a module for Gunicorn
from viyv_mcp import ViyvMCP
from app.config import Config
stateless_http = Config.get_stateless_http()
app = ViyvMCP("Production Server", stateless_http=stateless_http).get_app()
# Run with Gunicorn
gunicorn test_app:app -w 4 -k uvicorn.workers.UvicornWorker -b 0.0.0.0:8000
Docker Deployment
FROM python:3.10-slim
WORKDIR /app
COPY . .
RUN pip install uv && uv sync
CMD ["uv", "run", "gunicorn", "main:app", "-w", "4", "-k", "uvicorn.workers.UvicornWorker", "-b", "0.0.0.0:8000"]
๐ Performance Optimization
Caching Strategies
- Tools are cached per request
- External MCP connections are persistent
- Static file serving with efficient caching headers
Resource Management
- Automatic cleanup of external MCP processes
- Connection pooling for external services
- Graceful shutdown handling
Monitoring & Debugging
# Enable debug logging
LOG_LEVEL=DEBUG uv run python main.py
# Health check endpoint
curl http://localhost:8000/health
๐ Troubleshooting
Common Issues
SSE Streaming Not Working
- Ensure no middleware interferes with
/mcppath - Check ASGI routing configuration
- Verify FastMCP version >= 2.12.3
Multi-Worker Startup Failures
- Enable
STATELESS_HTTP=truefor multi-worker mode - Use Gunicorn instead of uvicorn's
--workersflag - Check for asyncio event loop conflicts
External MCP Server Issues
# Check external server logs
tail -f logs/external_mcp.log
# Verify command exists
which npx
# Test configuration
cat app/mcp_server_configs/test.json | jq .
Protocol Compatibility
- Use MCP protocol version 2024-11-05
- Pydantic v2 compatibility is patched automatically
- Check
mcp_initialize_fix.pyfor validation patches
๐ Examples
Complete working examples in the example/ directory:
claude_code_mcp: Claude Code CLI integrationtest: Comprehensive example with all features- External MCP servers
- Custom endpoints
๐ค Contributing
We welcome contributions! Please see our Contributing Guide.
Development Setup
# Clone the repository
git clone https://github.com/BrainFiber/viyv_mcp
cd viyv_mcp
# Install in development mode
pip install -e .
# Run tests
pytest
# Build package
python -m build
# Run example project
cd example/test
uv sync
STATELESS_HTTP=true uv run python main.py
Testing Guidelines
- Add sample implementations in
test/directory - Test with both session and stateless modes
- Check external MCP server bridging
๐ License
MIT License - see the LICENSE file for details.
๐ฅ Authors
- Hiroki Takezawa - BrainFiber
๐ Acknowledgments
- Built on FastMCP by jlowin
- Powered by Starlette ASGI framework
- Implements Model Context Protocol specification
๐ฎ Support
- ๐ง Email: hiroki.takezawa@brainfiber.net
- ๐ Issues: GitHub Issues
- ๐ฌ Discussions: GitHub Discussions
- ๐ Documentation: Wiki
๐ฆ Roadmap
- WebSocket support for real-time communication
- Built-in authentication/authorization
- Tool versioning and migration support
- Performance profiling dashboard
- Plugin system for custom integrations
- GraphQL endpoint support
๐ Changelog
v0.1.14 (Latest - 2025-10-13)
- ๐ Implementation Examples: Added comprehensive tool grouping examples in
example/test- 9 internal tools with group organization (Math, Statistics, Web Search, Image Tools)
- Real-world Playwright MCP server configuration (20 browser automation tools)
- ๐ Enhanced Documentation:
GROUPING_IMPLEMENTATION.md- detailed implementation reportapp/mcp_server_configs/README.md- external MCP server grouping guide- Sample configuration files for learning
- โ Verified Implementation: All examples tested and working with MCP Inspector
- ๐ฏ Makes tool grouping feature (v0.1.13) immediately usable with practical examples
v0.1.13
- ๐ Tool Grouping: Organize tools with
groupparameter in@tooland@agentdecorators - ๐ท๏ธ Vendor Namespace: Uses
_meta.viyv.groupfor MCP spec compliance - ๐ External MCP Grouping: Support
groupandgroup_mapinmcp_server_configs/*.json - โจ Optional Parameters: Added
titleanddestructivehints - ๐ Backward Compatible: Tools without groups work normally
- ๐ Enhanced templates and documentation with grouping examples
v0.1.10
- โจ Added stateless HTTP support for multi-worker deployments
- ๐ง Improved ASGI-level routing for SSE streaming
- ๐ฆ Updated FastMCP to 2.12.3 for better compatibility
- ๐ Fixed Pydantic v2 validation issues
- ๐ Enhanced documentation and examples
v0.1.9
- ๐ External MCP server bridging with tags and filtering
- ๐ Dynamic tool refresh on every request
Made with โค๏ธ by the viyv_mcp community
