feat: add /v1/** api

This commit is contained in:
Sun-ZhenXing
2025-07-19 22:50:04 +08:00
parent 43c7a970dd
commit bcb5994356
18 changed files with 177 additions and 44 deletions

View File

@@ -1,2 +1,2 @@
__version__ = "0.1.0"
__module_name__ = "mcp_template_python"
__version__ = "0.1.0"
__module_name__ = "mcp_template_python"

View File

@@ -1,5 +1,5 @@
from .__about__ import __version__
__all__ = [
"__version__",
]
from .__about__ import __version__
__all__ = [
"__version__",
]

View File

@@ -2,7 +2,8 @@ import argparse
import sys
from .__about__ import __module_name__, __version__
from .config import MCP_MAP, settings
from .app import MCP_MAP
from .config import settings
def main():

View File

@@ -0,0 +1,5 @@
from .math import mcp as math_mcp
MCP_MAP = {
"math": math_mcp,
}

View File

@@ -2,7 +2,9 @@ from operator import add, mul, sub, truediv
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("math")
from ..config import settings
mcp = FastMCP("math", settings=settings.instructions)
@mcp.tool()

View File

@@ -1,10 +1,4 @@
from pydantic_settings import BaseSettings
from .app.math import mcp as math
MCP_MAP = {
"math": math,
}
from pydantic_settings import BaseSettings, SettingsConfigDict
class Settings(BaseSettings):
@@ -16,9 +10,14 @@ class Settings(BaseSettings):
default_host: str = "127.0.0.1"
default_port: int = 3001
class Config:
env_file = ".env"
env_file_encoding = "utf-8"
instructions: str | None = None
model_config = SettingsConfigDict(
env_prefix="MCP_",
env_file=".env",
env_file_encoding="utf-8",
extra="allow",
)
settings = Settings()

View File

@@ -0,0 +1,9 @@
from typing import Any, Dict
from pydantic import BaseModel
class ArgumentsRequest(BaseModel):
"""Request model for tool and prompt calls."""
arguments: Dict[str, Any]

View File

@@ -0,0 +1,66 @@
from fastapi import APIRouter
from ..app import MCP_MAP
from ..models.helpers import ArgumentsRequest
router = APIRouter(prefix="/v1", tags=["helpers"])
@router.get("/details")
async def details():
"""Get details about all available tools, prompts, and resources."""
tools = [
{
"name": name,
"server_name": mcp.name,
"dependencies": mcp.dependencies,
"instructions": mcp.instructions,
"prompts": await mcp.list_prompts(),
"tools": await mcp.list_tools(),
"resources": await mcp.list_resources(),
"resource_templates": await mcp.list_resource_templates(),
}
for name, mcp in MCP_MAP.items()
]
return {"tools": tools}
@router.post("/mcps/{mcp_name}/tools/{tool_name}/call")
async def call_tool(
mcp_name: str,
tool_name: str,
request: ArgumentsRequest,
):
"""Call a specific tool with parameters."""
if mcp_name not in MCP_MAP:
return {"error": "MCP not found"}
mcp = MCP_MAP[mcp_name]
result = await mcp.call_tool(tool_name, request.arguments)
return result
@router.post("/mcps/{mcp_name}/prompts/{prompt_name}/call")
async def call_prompt(
mcp_name: str,
prompt_name: str,
request: ArgumentsRequest,
):
"""Call a specific prompt with parameters."""
if mcp_name not in MCP_MAP:
return {"error": "MCP not found"}
mcp = MCP_MAP[mcp_name]
result = await mcp.get_prompt(prompt_name, request.arguments)
return result
@router.get("/mcps/{mcp_name}/resources/{uri}")
async def get_resource(mcp_name: str, uri: str):
"""Get a specific resource."""
if mcp_name not in MCP_MAP:
return {"error": "MCP not found"}
mcp = MCP_MAP[mcp_name]
result = await mcp.read_resource(uri)
return result

View File

@@ -2,7 +2,8 @@ import contextlib
from fastapi import FastAPI
from .config import MCP_MAP
from .app import MCP_MAP
from .routers.helpers import router as helpers_router
@contextlib.asynccontextmanager
@@ -18,14 +19,21 @@ app = FastAPI(lifespan=lifespan)
@app.get("/")
async def root():
return {"message": "Welcome to the MCP Template Python Server!"}
"""Root endpoint."""
return {"message": "Welcome!"}
@app.get("/health")
async def health():
return {"status": "healthy"}
"""Check the health of the server and list available tools."""
return {
"status": "healthy",
"tools": list(MCP_MAP.keys()),
}
app.include_router(helpers_router)
for name, mcp in MCP_MAP.items():
app.mount(f"/{name}/compatible", mcp.sse_app())
app.mount(f"/{name}", mcp.streamable_http_app())