Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions src/fastmcp/prompts/prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ def __init__(
self,
content: Any,
role: Literal["user", "assistant"] = "user",
**kwargs: Any,
):
"""Create Message with automatic serialization.

Expand All @@ -88,7 +87,7 @@ def __init__(
serialized = pydantic_core.to_json(content, fallback=str).decode()
normalized_content = TextContent(type="text", text=serialized)

super().__init__(role=role, content=normalized_content, **kwargs)
super().__init__(role=role, content=normalized_content)

def to_mcp_prompt_message(self) -> PromptMessage:
"""Convert to MCP PromptMessage."""
Expand Down Expand Up @@ -148,7 +147,6 @@ def __init__(
messages: str | list[Message],
description: str | None = None,
meta: dict[str, Any] | None = None,
**kwargs: Any,
):
"""Create PromptResult.

Expand All @@ -158,9 +156,7 @@ def __init__(
meta: Optional metadata about the prompt result.
"""
normalized = self._normalize_messages(messages)
super().__init__(
messages=normalized, description=description, meta=meta, **kwargs
)
super().__init__(messages=normalized, description=description, meta=meta)

@staticmethod
def _normalize_messages(
Expand Down
12 changes: 3 additions & 9 deletions src/fastmcp/resources/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@
from fastmcp.server.dependencies import without_injected_parameters
from fastmcp.server.tasks.config import TaskConfig
from fastmcp.utilities.components import FastMCPComponent
from fastmcp.utilities.types import (
get_fn_name,
)
from fastmcp.utilities.types import get_fn_name


class ResourceContent(pydantic.BaseModel):
Expand Down Expand Up @@ -65,7 +63,6 @@ def __init__(
content: Any,
mime_type: str | None = None,
meta: dict[str, Any] | None = None,
**kwargs: Any,
):
"""Create ResourceContent with automatic serialization.

Expand All @@ -88,9 +85,7 @@ def __init__(
normalized_content = pydantic_core.to_json(content, fallback=str).decode()
mime_type = mime_type or "application/json"

super().__init__(
content=normalized_content, mime_type=mime_type, meta=meta, **kwargs
)
super().__init__(content=normalized_content, mime_type=mime_type, meta=meta)

def to_mcp_resource_contents(
self, uri: AnyUrl | str
Expand Down Expand Up @@ -162,7 +157,6 @@ def __init__(
self,
contents: str | bytes | list[ResourceContent],
meta: dict[str, Any] | None = None,
**kwargs: Any,
):
"""Create ResourceResult.

Expand All @@ -171,7 +165,7 @@ def __init__(
meta: Optional metadata about the resource result.
"""
normalized = self._normalize_contents(contents)
super().__init__(contents=normalized, meta=meta, **kwargs)
super().__init__(contents=normalized, meta=meta)

@staticmethod
def _normalize_contents(
Expand Down
6 changes: 3 additions & 3 deletions src/fastmcp/server/event_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@
from mcp.server.streamable_http import EventCallback, EventId, EventMessage, StreamId
from mcp.server.streamable_http import EventStore as SDKEventStore
from mcp.types import JSONRPCMessage
from pydantic import BaseModel

from fastmcp.utilities.logging import get_logger
from fastmcp.utilities.types import FastMCPBaseModel

logger = get_logger(__name__)


class EventEntry(BaseModel):
class EventEntry(FastMCPBaseModel):
"""Stored event entry."""

event_id: str
stream_id: str
message: dict | None # JSONRPCMessage serialized to dict


class StreamEventList(BaseModel):
class StreamEventList(FastMCPBaseModel):
"""List of event IDs for a stream."""

event_ids: list[str]
Expand Down
15 changes: 8 additions & 7 deletions src/fastmcp/server/middleware/caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
from key_value.aio.wrappers.statistics.wrapper import (
KVStoreCollectionStatistics,
)
from pydantic import BaseModel, Field
from pydantic import Field
from typing_extensions import NotRequired, Self, override

from fastmcp.prompts.prompt import Message, Prompt, PromptResult
from fastmcp.resources.resource import Resource, ResourceContent, ResourceResult
from fastmcp.server.middleware.middleware import CallNext, Middleware, MiddlewareContext
from fastmcp.tools.tool import Tool, ToolResult
from fastmcp.utilities.logging import get_logger
from fastmcp.utilities.types import FastMCPBaseModel

logger: Logger = get_logger(name=__name__)

Expand All @@ -34,15 +35,15 @@
GLOBAL_KEY = "__global__"


class CachableResourceContent(BaseModel):
class CachableResourceContent(FastMCPBaseModel):
"""A wrapper for ResourceContent that can be cached."""

content: str | bytes
mime_type: str | None = None
meta: dict[str, Any] | None = None


class CachableResourceResult(BaseModel):
class CachableResourceResult(FastMCPBaseModel):
"""A wrapper for ResourceResult that can be cached."""

contents: list[CachableResourceContent]
Expand Down Expand Up @@ -75,7 +76,7 @@ def unwrap(self) -> ResourceResult:
)


class CachableToolResult(BaseModel):
class CachableToolResult(FastMCPBaseModel):
content: list[mcp.types.ContentBlock]
structured_content: dict[str, Any] | None
meta: dict[str, Any] | None
Expand All @@ -96,14 +97,14 @@ def unwrap(self) -> ToolResult:
)


class CachableMessage(BaseModel):
class CachableMessage(FastMCPBaseModel):
"""A wrapper for Message that can be cached."""

role: str
content: mcp.types.TextContent | mcp.types.EmbeddedResource


class CachablePromptResult(BaseModel):
class CachablePromptResult(FastMCPBaseModel):
"""A wrapper for PromptResult that can be cached."""

messages: list[CachableMessage]
Expand Down Expand Up @@ -168,7 +169,7 @@ class GetPromptSettings(SharedMethodSettings):
"""Configuration options for Prompt-related caching."""


class ResponseCachingStatistics(BaseModel):
class ResponseCachingStatistics(FastMCPBaseModel):
list_tools: KVStoreCollectionStatistics | None = Field(default=None)
list_resources: KVStoreCollectionStatistics | None = Field(default=None)
list_prompts: KVStoreCollectionStatistics | None = Field(default=None)
Expand Down
5 changes: 3 additions & 2 deletions src/fastmcp/server/sampling/sampling_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
from typing import Any

from mcp.types import Tool as SDKTool
from pydantic import BaseModel, ConfigDict
from pydantic import ConfigDict

from fastmcp.tools.tool import ParsedFunction
from fastmcp.utilities.types import FastMCPBaseModel


class SamplingTool(BaseModel):
class SamplingTool(FastMCPBaseModel):
"""A tool that can be used during LLM sampling.

SamplingTools bundle a tool's schema (name, description, parameters) with
Expand Down
4 changes: 1 addition & 3 deletions src/fastmcp/tools/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,7 @@ def __init__(
)

super().__init__(
content=converted_content,
structured_content=structured_content,
meta=meta,
content=converted_content, structured_content=structured_content, meta=meta
)

def to_mcp_result(
Expand Down