models.py•2.74 kB
# app/models.py
from pydantic import BaseModel, Field
from typing import Optional, Dict, Any
# --- MCP Request Models ---
class MCPRequestParameters(BaseModel):
"""Parameters specific to a tool within an MCP request."""
location: str = Field(...,
description="The city name for which to fetch weather data (e.g., 'London' or 'New York, US').",
examples=["London,UK", "Tokyo"])
class MCPRequest(BaseModel):
"""Defines the structure for an incoming MCP request."""
protocol_version: str = Field(default="1.0", description="Version of the MCP protocol.")
tool_id: str = Field(description="Identifier for the tool being called.")
method: str = Field(description="The method to be executed by the tool.")
parameters: MCPRequestParameters = Field(..., description="Parameters required by the method.")
# You could add other generic MCP fields here if needed, e.g., request_id: Optional[str] = None
# --- MCP Response Models ---
class MCPWeatherData(BaseModel):
"""Structure for the weather data returned in a successful MCP response."""
location: str
temperature_celsius: float
temperature_fahrenheit: Optional[float] = None # Optional if conversion fails or not applicable
condition: str
description: str
humidity_percent: float
wind_kph: float
pressure_hpa: float
# You can add more fields here as you extract them from the weather API
# e.g., feels_like_celsius: Optional[float] = None
# icon_url: Optional[str] = None
class MCPResponse(BaseModel):
"""Defines the structure for an outgoing MCP response."""
protocol_version: str = "1.0"
tool_id: str # Should match the tool_id from the request
status: str = Field(description="'success' or 'error'")
data: Optional[MCPWeatherData] = None
error_message: Optional[str] = None
# Example of a model validator if you want to ensure data is present on success
# and error_message is present on error. (More advanced, can add later if needed)
# from pydantic import root_validator
# @root_validator
# def check_data_or_error(cls, values):
# status, data, error_message = values.get('status'), values.get('data'), values.get('error_message')
# if status == "success" and data is None:
# raise ValueError("Data must be provided for a successful response.")
# if status == "error" and error_message is None:
# raise ValueError("Error message must be provided for an error response.")
# if status == "success" and error_message is not None:
# raise ValueError("Error message should not be present for a successful response.")
# return values