π The fast, Pythonic way to build MCP servers and clients.
Refactor remote servers
Jeremiah Lowind27141a935f4
diff --git a/pyproject.toml b/pyproject.tomlindex f5b6c5fd3..a2a2297ed 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -11,6 +11,7 @@ dependencies = ["websockets>=15.0.1","fastapi>=0.115.12","openapi-pydantic>=0.5.1",+ "exceptiongroup>=1.2.2",]requires-python = ">=3.10"readme = "README.md"diff --git a/src/fastmcp/client/client.py b/src/fastmcp/client/client.pyindex dbae1b965..c49472d2b 100644--- a/src/fastmcp/client/client.py+++ b/src/fastmcp/client/client.py@@ -17,6 +17,7 @@ from fastmcp.client.roots import (create_roots_callback,)from fastmcp.client.sampling import SamplingHandler, create_sampling_callback+from fastmcp.exceptions import ClientErrorfrom fastmcp.server import FastMCPfrom .transports import ClientTransport, SessionKwargs, infer_transport@@ -24,10 +25,6 @@ from .transports import ClientTransport, SessionKwargs, infer_transport__all__ = ["Client", "RootsHandler", "RootsList"]-class ClientError(ValueError):- """Base class for errors raised by the client."""--class Client:"""MCP client that delegates connection management to a Transport instance.@@ -48,7 +45,7 @@ class Client:):self.transport = infer_transport(transport)self._session: ClientSession | None = None- self._session_cm: AbstractAsyncContextManager[ClientSession] | None = None+ self._session_cms: list[AbstractAsyncContextManager[ClientSession]] = []self._session_kwargs: SessionKwargs = {"sampling_callback": None,@@ -89,24 +86,28 @@ class Client:async def __aenter__(self):if self.is_connected():- raise RuntimeError("Client is already connected in an async context.")+ # We're already connected, no need to add None to the session_cms list+ return self+try:- self._session_cm = self.transport.connect_session(**self._session_kwargs)- self._session = await self._session_cm.__aenter__()+ session_cm = self.transport.connect_session(**self._session_kwargs)+ self._session_cms.append(session_cm)+ self._session = await self._session_cms[-1].__aenter__()return selfexcept Exception as e:# Ensure cleanup if __aenter__ fails partiallyself._session = None- self._session_cm = None+ if self._session_cms:+ self._session_cms.pop()raise ConnectionError(f"Failed to connect using {self.transport}: {e}") from easync def __aexit__(self, exc_type, exc_val, exc_tb):- if self._session_cm:- await self._session_cm.__aexit__(exc_type, exc_val, exc_tb)- self._session = None- self._session_cm = None+ if self._session_cms:+ await self._session_cms[-1].__aexit__(exc_type, exc_val, exc_tb)+ self._session = None+ self._session_cms.pop()# --- MCP Client Methods ---async def ping(self) -> None:@@ -168,10 +169,10 @@ class Client:async def get_prompt(self, name: str, arguments: dict[str, str] | None = None- ) -> mcp.types.GetPromptResult:+ ) -> list[mcp.types.PromptMessage]:"""Send a prompts/get request."""result = await self.session.get_prompt(name, arguments)- return result+ return result.messagesasync def complete(self,diff --git a/src/fastmcp/client/transports.py b/src/fastmcp/client/transports.pyindex 27b885688..8817b134a 100644--- a/src/fastmcp/client/transports.py+++ b/src/fastmcp/client/transports.py@@ -8,7 +8,8 @@ from typing import (TypedDict,)-from mcp import ClientSession, StdioServerParameters+from exceptiongroup import BaseExceptionGroup, catch+from mcp import ClientSession, McpError, StdioServerParametersfrom mcp.client.session import (ListRootsFnT,LoggingFnT,@@ -22,6 +23,7 @@ from mcp.shared.memory import create_connected_server_and_client_sessionfrom pydantic import AnyUrlfrom typing_extensions import Unpack+from fastmcp.exceptions import ClientErrorfrom fastmcp.server import FastMCP as FastMCPServer@@ -382,12 +384,26 @@ class FastMCPTransport(ClientTransport):async def connect_session(self, **session_kwargs: Unpack[SessionKwargs]) -> AsyncIterator[ClientSession]:- # create_connected_server_and_client_session manages the session lifecycle itself- async with create_connected_server_and_client_session(- server=self._fastmcp._mcp_server,- **session_kwargs,- ) as session:- yield session+ def exception_handler(excgroup: BaseExceptionGroup):+ for exc in excgroup.exceptions:+ if isinstance(exc, BaseExceptionGroup):+ exception_handler(exc)+ raise exc++ def mcperror_handler(excgroup: BaseExceptionGroup):+ for exc in excgroup.exceptions:+ if isinstance(exc, BaseExceptionGroup):+ mcperror_handler(exc)+ raise ClientError(exc)++ # backport of 3.11's except* syntax+ with catch({McpError: mcperror_handler, Exception: exception_handler}):+ # create_connected_server_and_client_session manages the session lifecycle itself+ async with create_connected_server_and_client_session(+ server=self._fastmcp._mcp_server,+ **session_kwargs,+ ) as session:+ yield sessiondef __repr__(self) -> str:return f"<FastMCP(server='{self._fastmcp.name}')>"diff --git a/src/fastmcp/exceptions.py b/src/fastmcp/exceptions.pyindex 1314ef615..c105cce8e 100644--- a/src/fastmcp/exceptions.py+++ b/src/fastmcp/exceptions.py@@ -23,3 +23,11 @@ class PromptError(FastMCPError):class InvalidSignature(Exception):"""Invalid signature for use with FastMCP."""+++class ClientError(Exception):+ """Error in client operations."""+++class NotFoundError(Exception):+ """Object not found."""diff --git a/src/fastmcp/prompts/prompt.py b/src/fastmcp/prompts/prompt.pyindex 5733b3705..c6bcefc62 100644--- a/src/fastmcp/prompts/prompt.py+++ b/src/fastmcp/prompts/prompt.py@@ -115,7 +115,7 @@ class Prompt(BaseModel):return cls(name=func_name,- description=description or fn.__doc__ or "",+ description=description or fn.__doc__,arguments=arguments,fn=fn,tags=tags or set(),diff --git a/src/fastmcp/prompts/prompt_manager.py b/src/fastmcp/prompts/prompt_manager.pyindex ce423c731..f223d1b32 100644--- a/src/fastmcp/prompts/prompt_manager.py+++ b/src/fastmcp/prompts/prompt_manager.py@@ -3,8 +3,8 @@from collections.abc import Awaitable, Callablefrom typing import Any-from fastmcp.exceptions import PromptError-from fastmcp.prompts.prompt import MCPPrompt, Message, Prompt, PromptResult+from fastmcp.exceptions import NotFoundError+from fastmcp.prompts.prompt import Message, Prompt, PromptResultfrom fastmcp.settings import DuplicateBehaviorfrom fastmcp.utilities.logging import get_logger@@ -37,16 +37,6 @@ class PromptManager:"""Get all registered prompts, indexed by registered key."""return self._prompts- def list_prompts(self) -> list[Prompt]:- """List all registered prompts."""- return list(self.get_prompts().values())-- def list_mcp_prompts(self) -> list[MCPPrompt]:- """List all registered prompts in the format expected by the low-level MCP server."""- return [- prompt.to_mcp_prompt(name=key) for key, prompt in self.get_prompts().items()- ]-def add_prompt_from_fn(self,fn: Callable[..., PromptResult | Awaitable[PromptResult]],@@ -84,28 +74,6 @@ class PromptManager:"""Render a prompt by name with arguments."""prompt = self.get_prompt(name)if not prompt:- raise PromptError(f"Unknown prompt: {name}")+ raise NotFoundError(f"Unknown prompt: {name}")return await prompt.render(arguments)-- def import_prompts(- self, manager: "PromptManager", prefix: str | None = None- ) -> None:- """- Import all prompts from another PromptManager with prefixed names.-- Args:- manager: Another PromptManager instance to import prompts from- prefix: Prefix to add to prompt names. The resulting prompt key will- be in the format "{prefix}{original_name}" if prefix is provided,- otherwise the original name is used.- For example, with prefix "weather/" and prompt "forecast_prompt",- the imported prompt would be available as "weather/forecast_prompt"- """- for name, prompt in manager._prompts.items():- # Create prefixed key- key = f"{prefix}{name}" if prefix else name-- # Store the prompt with the prefixed key- self.add_prompt(prompt, key=key)- logger.debug(f'Imported prompt "{prompt.name}" as "{key}"')diff --git a/src/fastmcp/resources/resource_manager.py b/src/fastmcp/resources/resource_manager.pyindex 7f3256a76..4a83a2315 100644--- a/src/fastmcp/resources/resource_manager.py+++ b/src/fastmcp/resources/resource_manager.py@@ -6,11 +6,10 @@ from typing import Anyfrom pydantic import AnyUrl-from fastmcp.exceptions import ResourceError+from fastmcp.exceptions import NotFoundErrorfrom fastmcp.resources import FunctionResource-from fastmcp.resources.resource import MCPResource, Resource+from fastmcp.resources.resource import Resourcefrom fastmcp.resources.template import (- MCPResourceTemplate,ResourceTemplate,match_uri_template,)@@ -207,7 +206,7 @@ class ResourceManager:"""Get resource by URI, checking concrete resources first, then templates.Raises:- ResourceError: If no resource or template matching the URI is found.+ NotFoundError: If no resource or template matching the URI is found."""uri_str = str(uri)logger.debug("Getting resource", extra={"uri": uri_str})@@ -225,85 +224,12 @@ class ResourceManager:except Exception as e:raise ValueError(f"Error creating resource from template: {e}")- raise ResourceError(f"Unknown resource: {uri_str}")+ raise NotFoundError(f"Unknown resource: {uri_str}")def get_resources(self) -> dict[str, Resource]:"""Get all registered resources, keyed by URI."""return self._resources- def list_resources(self) -> list[Resource]:- """List all registered resources."""- logger.debug("Listing resources", extra={"count": len(self._resources)})- return list(self._resources.values())-- def list_mcp_resources(self) -> list[MCPResource]:- """List all registered resources in the format expected by the low-level MCP server."""-- return [- resource.to_mcp_resource(uri=key)- for key, resource in self._resources.items()- ]-- def list_mcp_resource_templates(self) -> list[MCPResourceTemplate]:- """List all registered resource templates in the format expected by the low-level MCP server."""- return [- template.to_mcp_template(uriTemplate=key)- for key, template in self._templates.items()- ]-def get_templates(self) -> dict[str, ResourceTemplate]:"""Get all registered templates, keyed by URI template."""return self._templates-- def list_templates(self) -> list[ResourceTemplate]:- """List all registered templates."""- logger.debug("Listing templates", extra={"count": len(self._templates)})- return list(self._templates.values())-- def import_resources(- self, manager: "ResourceManager", prefix: str | None = None- ) -> None:- """Import resources from another resource manager.-- Resources are imported with a prefixed URI if a prefix is provided. For example,- if a resource has URI "data://users" and you import it with prefix "app+", the- imported resource will have URI "app+data://users". If no prefix is provided,- the original URI is used.-- Args:- manager: The ResourceManager to import from- prefix: A prefix to apply to the resource URIs, including the delimiter.- For example, "app+" would result in URIs like "app+data://users".- If None, the original URI is used.- """- for uri, resource in manager._resources.items():- # Create prefixed URI and import the resource with the new URI as the storage key- prefixed_uri = f"{prefix}{uri}" if prefix else uri- self.add_resource(resource, key=prefixed_uri)- logger.debug(f'Imported resource "{uri}" as "{prefixed_uri}"')-- def import_templates(- self, manager: "ResourceManager", prefix: str | None = None- ) -> None:- """Import resource templates from another resource manager.-- Templates are imported with a prefixed URI template if a prefix is provided.- For example, if a template has URI template "data://users/{id}" and you import- it with prefix "app+", the imported template will have URI template- "app+data://users/{id}". If no prefix is provided, the original URI template is used.-- Args:- manager: The ResourceManager to import templates from- prefix: A prefix to apply to the template URIs, including the delimiter.- For example, "app+" would result in URI templates like "app+data://users/{id}".- If None, the original URI template is used.- """- for uri_template, template in manager._templates.items():- # Create prefixed URI template and import the template with the new URI as the storage key- prefixed_uri_template = (- f"{prefix}{uri_template}" if prefix else uri_template- )- self.add_template(template, key=prefixed_uri_template)- logger.debug(- f'Imported template "{uri_template}" as "{prefixed_uri_template}"'- )diff --git a/src/fastmcp/server/openapi.py b/src/fastmcp/server/openapi.pyindex d25dc314d..bc65c2ff7 100644--- a/src/fastmcp/server/openapi.py+++ b/src/fastmcp/server/openapi.py@@ -8,6 +8,7 @@ from re import Patternfrom typing import Any, Literalimport httpx+from mcp.types import TextContentfrom pydantic.networks import AnyUrlfrom fastmcp.resources import Resource, ResourceTemplate@@ -613,25 +614,18 @@ class FastMCPOpenAPI(FastMCP):f"Registered TEMPLATE: {uri_template_str} ({route.method} {route.path}) with tags: {route.tags}")- async def call_tool(self, name: str, arguments: dict[str, Any]) -> Any:- """Override the call_tool method to return the raw result without converting to content.+ async def _mcp_call_tool(self, name: str, arguments: dict[str, Any]) -> Any:+ """Override the call_tool method to return the raw result without converting to content."""- For testing purposes, if specific tools are called, we convert the result to the expected object.- """context = self.get_context()result = await self._tool_manager.call_tool(name, arguments, context=context)- # For testing purposes, convert result to expected model based on tool name- if name == "create_user_users_post":- # Try to import User class from test module- try:- from tests.server.test_openapi import User-- # Convert dict to User object- if isinstance(result, dict):- return User(**result)- except ImportError:- # If User class not found, just return the raw result- pass+ # For other tools, ensure the response is wrapped in TextContent+ if isinstance(result, dict | str):+ if isinstance(result, dict):+ result_text = json.dumps(result)+ else:+ result_text = result+ return [TextContent(text=result_text, type="text")]return resultdiff --git a/src/fastmcp/server/proxy.py b/src/fastmcp/server/proxy.pyindex 887e60efc..e4c31e5c2 100644--- a/src/fastmcp/server/proxy.py+++ b/src/fastmcp/server/proxy.py@@ -2,10 +2,19 @@ from typing import Any, castfrom urllib.parse import quoteimport mcp.types-from mcp.types import BlobResourceContents, TextResourceContents+from mcp.server.lowlevel.helper_types import ReadResourceContents+from mcp.types import (+ BlobResourceContents,+ EmbeddedResource,+ GetPromptResult,+ ImageContent,+ TextContent,+ TextResourceContents,+)+from pydantic.networks import AnyUrl-import fastmcpfrom fastmcp.client import Client+from fastmcp.exceptions import NotFoundErrorfrom fastmcp.prompts import Message, Promptfrom fastmcp.resources import Resource, ResourceTemplatefrom fastmcp.server.context import Context@@ -152,79 +161,89 @@ class ProxyPrompt(Prompt):async def render(self, arguments: dict[str, Any]) -> list[Message]:async with self._client:result = await self._client.get_prompt(self.name, arguments)- return [Message(role=m.role, content=m.content) for m in result.messages]+ return [Message(role=m.role, content=m.content) for m in result]class FastMCPProxy(FastMCP):- def __init__(self, _async_constructor: bool, **kwargs):- if not _async_constructor:- raise ValueError(- "FastMCPProxy() was initialied unexpectedly. Please use a constructor like `FastMCPProxy.from_client()` instead."- )+ def __init__(self, client: "Client", **kwargs):super().__init__(**kwargs)-- @classmethod- async def from_client(- cls,- client: "Client",- name: str | None = None,- **settings: fastmcp.settings.ServerSettings,- ) -> "FastMCPProxy":- """Create a FastMCP proxy server from a client.-- This method creates a new FastMCP server instance that proxies requests to the provided client.- It discovers the client's tools, resources, prompts, and templates, and creates corresponding- components in the server that forward requests to the client.-- Args:- client: The client to proxy requests to- name: Optional name for the new FastMCP server (defaults to client name if available)- **settings: Additional settings for the FastMCP server-- Returns:- A FastMCP server that proxies requests to the client- """- server = cls(name=name, **settings, _async_constructor=True)-- async with client:- # Register proxies for client tools- tools = await client.list_tools()- for tool in tools:- tool_proxy = await ProxyTool.from_client(client, tool)- server._tool_manager._tools[tool_proxy.name] = tool_proxy- logger.debug(f"Created proxy for tool: {tool_proxy.name}")-- # Register proxies for client resources- resources = await client.list_resources()- for resource in resources:- resource_proxy = await ProxyResource.from_client(client, resource)- server._resource_manager._resources[str(resource_proxy.uri)] = (- resource_proxy- )- logger.debug(f"Created proxy for resource: {resource_proxy.uri}")-- # Register proxies for client resource templates- templates = await client.list_resource_templates()- for template in templates:- template_proxy = await ProxyTemplate.from_client(client, template)- server._resource_manager._templates[template_proxy.uri_template] = (- template_proxy- )- logger.debug(- f"Created proxy for template: {template_proxy.uri_template}"- )-- # Register proxies for client prompts- prompts = await client.list_prompts()- for prompt in prompts:- prompt_proxy = await ProxyPrompt.from_client(client, prompt)- server._prompt_manager._prompts[prompt_proxy.name] = prompt_proxy- logger.debug(f"Created proxy for prompt: {prompt_proxy.name}")-- logger.info(f"Created server '{server.name}' proxying to client: {client}")- return server-- @classmethod- async def from_server(cls, server: FastMCP, **settings: Any) -> "FastMCPProxy":- client = Client(transport=fastmcp.client.transports.FastMCPTransport(server))- return await cls.from_client(client, **settings)+ self.client = client++ async def get_tools(self) -> dict[str, Tool]:+ tools = await super().get_tools()++ async with self.client:+ for tool in await self.client.list_tools():+ tool_proxy = await ProxyTool.from_client(self.client, tool)+ tools[tool_proxy.name] = tool_proxy++ return tools++ async def get_resources(self) -> dict[str, Resource]:+ resources = await super().get_resources()++ async with self.client:+ for resource in await self.client.list_resources():+ resource_proxy = await ProxyResource.from_client(self.client, resource)+ resources[str(resource_proxy.uri)] = resource_proxy++ return resources++ async def get_resource_templates(self) -> dict[str, ResourceTemplate]:+ templates = await super().get_resource_templates()++ async with self.client:+ for template in await self.client.list_resource_templates():+ template_proxy = await ProxyTemplate.from_client(self.client, template)+ templates[template_proxy.uri_template] = template_proxy++ return templates++ async def get_prompts(self) -> dict[str, Prompt]:+ prompts = await super().get_prompts()++ async with self.client:+ for prompt in await self.client.list_prompts():+ prompt_proxy = await ProxyPrompt.from_client(self.client, prompt)+ prompts[prompt_proxy.name] = prompt_proxy+ return prompts++ async def _mcp_call_tool(+ self, key: str, arguments: dict[str, Any]+ ) -> list[TextContent | ImageContent | EmbeddedResource]:+ try:+ result = await super()._mcp_call_tool(key, arguments)+ return result+ except NotFoundError:+ async with self.client:+ result = await self.client.call_tool(key, arguments)+ return result++ async def _mcp_read_resource(self, uri: AnyUrl | str) -> list[ReadResourceContents]:+ try:+ result = await super()._mcp_read_resource(uri)+ return result+ except NotFoundError:+ async with self.client:+ resource = await self.client.read_resource(uri)+ if isinstance(resource[0], TextResourceContents):+ content = resource[0].text+ elif isinstance(resource[0], BlobResourceContents):+ content = resource[0].blob+ else:+ raise ValueError(f"Unsupported content type: {type(resource[0])}")++ return [+ ReadResourceContents(content=content, mime_type=resource[0].mimeType)+ ]++ async def _mcp_get_prompt(+ self, name: str, arguments: dict[str, Any] | None = None+ ) -> GetPromptResult:+ try:+ result = await super()._mcp_get_prompt(name, arguments)+ return result+ except NotFoundError:+ async with self.client:+ result = await self.client.get_prompt(name, arguments)+ return GetPromptResult(messages=result)diff --git a/src/fastmcp/server/server.py b/src/fastmcp/server/server.pyindex e7b267375..dc52a9f3a 100644--- a/src/fastmcp/server/server.py+++ b/src/fastmcp/server/server.py@@ -1,6 +1,6 @@"""FastMCP - A more ergonomic interface for MCP servers."""-import json+import datetimefrom collections.abc import AsyncIterator, Awaitable, Callablefrom contextlib import (AbstractAsyncContextManager,@@ -38,24 +38,114 @@ from starlette.routing import Mount, Routeimport fastmcpimport fastmcp.settings-from fastmcp.exceptions import ResourceError+from fastmcp.exceptions import NotFoundError, ResourceErrorfrom fastmcp.prompts import Prompt, PromptManager-from fastmcp.prompts.prompt import Message, PromptResult+from fastmcp.prompts.prompt import PromptResultfrom fastmcp.resources import Resource, ResourceManagerfrom fastmcp.resources.template import ResourceTemplatefrom fastmcp.tools import ToolManagerfrom fastmcp.tools.tool import Toolfrom fastmcp.utilities.decorators import DecoratedFunctionfrom fastmcp.utilities.logging import configure_logging, get_logger-from fastmcp.utilities.types import Imageif TYPE_CHECKING:from fastmcp.client import Clientfrom fastmcp.server.context import Contextfrom fastmcp.server.openapi import FastMCPOpenAPIfrom fastmcp.server.proxy import FastMCPProxy+logger = get_logger(__name__)+NOT_FOUND = object()+++class MountedServer:+ def __init__(+ self,+ prefix: str,+ server: "FastMCP",+ tool_separator: str | None = None,+ resource_separator: str | None = None,+ prompt_separator: str | None = None,+ cache_expiration_seconds: int = 10,+ ):+ if tool_separator is None:+ tool_separator = "_"+ if resource_separator is None:+ resource_separator = "+"+ if prompt_separator is None:+ prompt_separator = "_"++ self.server = server+ self.prefix = prefix+ self.tool_separator = tool_separator+ self.resource_separator = resource_separator+ self.prompt_separator = prompt_separator+ self.cache = TimedCache(+ expiration=datetime.timedelta(seconds=cache_expiration_seconds)+ )++ async def get_tools(self) -> dict[str, Tool]:+ cached_tools = self.cache.get("tools")+ if cached_tools is NOT_FOUND:+ self.cache.set("tools", {})+ cached_tools = await self.server.get_tools()+ self.cache.set("tools", cached_tools)+ return {+ f"{self.prefix}{self.tool_separator}{key}": tool+ for key, tool in cached_tools.items()+ }++ async def get_resources(self) -> dict[str, Resource]:+ cached_resources = self.cache.get("resources")+ if cached_resources is NOT_FOUND:+ self.cache.set("resources", {})+ cached_resources = await self.server.get_resources()+ self.cache.set("resources", cached_resources)+ return cached_resources++ async def get_resource_templates(self) -> dict[str, ResourceTemplate]:+ cached_templates = self.cache.get("resource_templates")+ if cached_templates is NOT_FOUND:+ self.cache.set("resource_templates", {})+ cached_templates = await self.server.get_resource_templates()+ self.cache.set("resource_templates", cached_templates)+ return cached_templates++ async def get_prompts(self) -> dict[str, Prompt]:+ cached_prompts = self.cache.get("prompts")+ if cached_prompts is NOT_FOUND:+ self.cache.set("prompts", {})+ cached_prompts = await self.server.get_prompts()+ self.cache.set("prompts", cached_prompts)+ return cached_prompts++ async def match_tool(self, key: str) -> bool:+ return key.startswith(f"{self.prefix}{self.tool_separator}")++ async def match_resource(self, key: str) -> bool:+ return key.startswith(f"{self.prefix}{self.resource_separator}")++ async def match_prompt(self, key: str) -> bool:+ return key.startswith(f"{self.prefix}{self.prompt_separator}")+++class TimedCache:+ def __init__(self, expiration: datetime.timedelta):+ self.expiration = expiration+ self.cache: dict[Any, tuple[Any, datetime.datetime]] = {}++ def set(self, key: Any, value: Any) -> None:+ expires = datetime.datetime.now() + self.expiration+ self.cache[key] = (value, expires)++ def get(self, key: Any) -> Any:+ value = self.cache.get(key)+ if value is not None and value[1] > datetime.datetime.now():+ return value[0]+ else:+ return NOT_FOUND+@asynccontextmanagerasync def default_lifespan(server: "FastMCP") -> AsyncIterator[Any]:@@ -70,7 +160,7 @@ async def default_lifespan(server: "FastMCP") -> AsyncIterator[Any]:yield {}-def lifespan_wrapper(+def _lifespan_wrapper(app: "FastMCP",lifespan: Callable[["FastMCP"], AbstractAsyncContextManager[LifespanResultT]],) -> Callable[@@ -79,17 +169,7 @@ def lifespan_wrapper(@asynccontextmanagerasync def wrap(s: MCPServer[LifespanResultT]) -> AsyncIterator[LifespanResultT]:async with AsyncExitStack() as stack:- # enter main app's lifespancontext = await stack.enter_async_context(lifespan(app))-- # Enter all mounted app lifespans- for prefix, mounted_app in app._mounted_apps.items():- mounted_context = mounted_app._mcp_server.lifespan(- mounted_app._mcp_server- )- await stack.enter_async_context(mounted_context)- logger.debug(f"Prepared lifespan for mounted app '{prefix}'")-yield contextreturn wrap@@ -108,9 +188,13 @@ class FastMCP(Generic[LifespanResultT]):):self.tags: set[str] = tags or set()self.settings = fastmcp.settings.ServerSettings(**settings)+ self._cache = TimedCache(+ expiration=datetime.timedelta(+ seconds=self.settings.cache_expiration_seconds+ )+ )- # Setup for mounted apps - must be initialized before _mcp_server- self._mounted_apps: dict[str, FastMCP] = {}+ self._server_mounts: dict[str, MountedServer] = {}if lifespan is None:lifespan = default_lifespan@@ -118,7 +202,7 @@ class FastMCP(Generic[LifespanResultT]):self._mcp_server = MCPServer[LifespanResultT](name=name or "FastMCP",instructions=instructions,- lifespan=lifespan_wrapper(self, lifespan),+ lifespan=_lifespan_wrapper(self, lifespan),)self._tool_manager = ToolManager(duplicate_behavior=self.settings.on_duplicate_tools@@ -137,6 +221,9 @@ class FastMCP(Generic[LifespanResultT]):# Configure loggingconfigure_logging(self.settings.log_level)+ def __repr__(self) -> str:+ return f"{type(self).__name__}({self.name!r})"+@propertydef name(self) -> str:return self._mcp_server.name@@ -177,26 +264,13 @@ class FastMCP(Generic[LifespanResultT]):def _setup_handlers(self) -> None:"""Set up core MCP protocol handlers."""self._mcp_server.list_tools()(self._mcp_list_tools)- self._mcp_server.call_tool()(self.call_tool)+ self._mcp_server.call_tool()(self._mcp_call_tool)self._mcp_server.list_resources()(self._mcp_list_resources)self._mcp_server.read_resource()(self._mcp_read_resource)self._mcp_server.list_prompts()(self._mcp_list_prompts)self._mcp_server.get_prompt()(self._mcp_get_prompt)self._mcp_server.list_resource_templates()(self._mcp_list_resource_templates)- def get_tools(self) -> dict[str, Tool]:- """Get all registered tools, indexed by registered key."""- return self._tool_manager.get_tools()-- async def _mcp_list_tools(self) -> list[MCPTool]:- """- List all available tools, in the format expected by the low-level MCP- server.-- See `list_tools` for a more ergonomic way to list tools.- """- return [tool.to_mcp_tool(name=key) for key, tool in self.get_tools().items()]-def get_context(self) -> "Context[ServerSession, LifespanResultT]":"""Returns a Context object. Note that the context will only be valid@@ -211,52 +285,101 @@ class FastMCP(Generic[LifespanResultT]):return Context(request_context=request_context, fastmcp=self)- async def call_tool(- self, key: str, arguments: dict[str, Any]- ) -> list[TextContent | ImageContent | EmbeddedResource]:- """Call a tool by name with arguments."""- context = self.get_context()- result = await self._tool_manager.call_tool(key, arguments, context=context)- converted_result = _convert_to_content(result)- return converted_result-- def get_resources(self) -> dict[str, Resource]:+ async def get_tools(self) -> dict[str, Tool]:+ """Get all registered tools, indexed by registered key."""+ tools = {}+ for server in self._server_mounts.values():+ server_tools = await server.get_tools()+ tools.update(server_tools)+ tools.update(self._tool_manager.get_tools())+ return tools++ async def get_resources(self) -> dict[str, Resource]:"""Get all registered resources, indexed by registered key."""- return self._resource_manager.get_resources()+ resources = {}+ for server in self._server_mounts.values():+ server_resources = await server.get_resources()+ resources.update(server_resources)+ resources.update(self._resource_manager.get_resources())+ return resources++ async def get_resource_templates(self) -> dict[str, ResourceTemplate]:+ """Get all registered resource templates, indexed by registered key."""+ templates = {}+ for server in self._server_mounts.values():+ server_templates = await server.get_resource_templates()+ templates.update(server_templates)+ templates.update(self._resource_manager.get_templates())+ return templates++ async def get_prompts(self) -> dict[str, Prompt]:+ """+ List all available prompts.+ """+ prompts = {}+ for server in self._server_mounts.values():+ server_prompts = await server.get_prompts()+ prompts.update(server_prompts)+ prompts.update(self._prompt_manager.get_prompts())+ return prompts- def list_resources(self) -> list[Resource]:- """List all registered resources."""- return self._resource_manager.list_resources()+ async def _mcp_list_tools(self) -> list[MCPTool]:+ """+ List all available tools, in the format expected by the low-level MCP+ server.++ """+ tools = await self.get_tools()+ return [tool.to_mcp_tool(name=key) for key, tool in tools.items()]async def _mcp_list_resources(self) -> list[MCPResource]:"""List all available resources, in the format expected by the low-level MCPserver.- See `list_resources` for a more ergonomic way to list resources."""-- return self._resource_manager.list_mcp_resources()-- def list_resource_templates(self) -> list[ResourceTemplate]:- return self._resource_manager.list_templates()+ resources = await self.get_resources()+ return [+ resource.to_mcp_resource(uri=key) for key, resource in resources.items()+ ]async def _mcp_list_resource_templates(self) -> list[MCPResourceTemplate]:"""List all available resource templates, in the format expected by the low-levelMCP server.- See `list_resource_templates` for a more ergonomic way to list resource- templates."""- return self._resource_manager.list_mcp_resource_templates()+ templates = await self.get_resource_templates()+ return [+ template.to_mcp_template(uriTemplate=key)+ for key, template in templates.items()+ ]- async def read_resource(self, uri: AnyUrl | str) -> str | bytes:- """Read a resource by URI."""- resource = await self._resource_manager.get_resource(uri)- if not resource:- raise ResourceError(f"Unknown resource: {uri}")- return await resource.read()+ async def _mcp_list_prompts(self) -> list[MCPPrompt]:+ """+ List all available prompts, in the format expected by the low-level MCP+ server.++ """+ prompts = await self.get_prompts()+ return [prompt.to_mcp_prompt(name=key) for key, prompt in prompts.items()]++ async def _mcp_call_tool(+ self, key: str, arguments: dict[str, Any]+ ) -> list[TextContent | ImageContent | EmbeddedResource]:+ """Call a tool by name with arguments."""+ if self._tool_manager.get_tool(key):+ context = self.get_context()+ result = await self._tool_manager.call_tool(key, arguments, context=context)++ else:+ for server in self._server_mounts.values():+ if server.match_tool(key):+ result = await server.server._mcp_call_tool(key, arguments)+ break+ else:+ raise NotFoundError(f"Unknown tool: {key}")+ return resultasync def _mcp_read_resource(self, uri: AnyUrl | str) -> list[ReadResourceContents]:"""@@ -265,17 +388,40 @@ class FastMCP(Generic[LifespanResultT]):See `read_resource` for a more ergonomic way to read resources."""+ if resource := await self._resource_manager.get_resource(uri):+ try:+ content = await resource.read()+ return [+ ReadResourceContents(content=content, mime_type=resource.mime_type)+ ]+ except Exception as e:+ logger.error(f"Error reading resource {uri}: {e}")+ raise ResourceError(str(e))+ else:+ for server in self._server_mounts.values():+ if server.match_resource(str(uri)):+ return await server.server._mcp_read_resource(uri)+ else:+ raise NotFoundError(f"Unknown resource: {uri}")- resource = await self._resource_manager.get_resource(uri)- if not resource:- raise ResourceError(f"Unknown resource: {uri}")+ async def _mcp_get_prompt(+ self, name: str, arguments: dict[str, Any] | None = None+ ) -> GetPromptResult:+ """+ Get a prompt by name with arguments, in the format expected by the low-level+ MCP server.- try:- content = await self.read_resource(uri)- return [ReadResourceContents(content=content, mime_type=resource.mime_type)]- except Exception as e:- logger.error(f"Error reading resource {uri}: {e}")- raise ResourceError(str(e))+ See `get_prompt` for a more ergonomic way to get prompts.+ """+ if prompt := self._prompt_manager.get_prompt(name):+ messages = await prompt.render(arguments)+ return GetPromptResult(messages=pydantic_core.to_jsonable_python(messages))+ else:+ for server in self._server_mounts.values():+ if server.match_prompt(name):+ return await server.server._mcp_get_prompt(name, arguments)+ else:+ raise NotFoundError(f"Unknown prompt: {name}")def add_tool(self,@@ -574,45 +720,30 @@ class FastMCP(Generic[LifespanResultT]):],)- def list_prompts(self) -> list[Prompt]:- """- List all available prompts.- """- return self._prompt_manager.list_prompts()-- async def _mcp_list_prompts(self) -> list[MCPPrompt]:- """- List all available prompts, in the format expected by the low-level MCP- server.-- See `list_prompts` for a more ergonomic way to list prompts.- """- return self._prompt_manager.list_mcp_prompts()-- async def get_prompt(- self, name: str, arguments: dict[str, Any] | None = None- ) -> list[Message]:- """Get a prompt by name with arguments."""- return await self._prompt_manager.render_prompt(name, arguments)-- async def _mcp_get_prompt(- self, name: str, arguments: dict[str, Any] | None = None- ) -> GetPromptResult:+ def mount(+ self,+ prefix: str,+ server: "FastMCP",+ tool_separator: str | None = None,+ resource_separator: str | None = None,+ prompt_separator: str | None = None,+ ) -> None:"""- Get a prompt by name with arguments, in the format expected by the low-level- MCP server.-- See `get_prompt` for a more ergonomic way to get prompts.+ Mount another FastMCP server on a given prefix."""- try:- messages = await self.get_prompt(name, arguments)+ mounted_server = MountedServer(+ server=server,+ prefix=prefix,+ tool_separator=tool_separator,+ resource_separator=resource_separator,+ prompt_separator=prompt_separator,+ )+ self._server_mounts[prefix] = mounted_server- return GetPromptResult(messages=pydantic_core.to_jsonable_python(messages))- except Exception as e:- logger.error(f"Error getting prompt {name}: {e}")- raise ValueError(str(e))+ def unmount(self, prefix: str) -> None:+ self._server_mounts.pop(prefix)- def import_server(+ async def import_server(self,prefix: str,server: "FastMCP",@@ -663,20 +794,20 @@ class FastMCP(Generic[LifespanResultT]):# Import tools from the mounted servertool_prefix = f"{prefix}{tool_separator}"- self._tool_manager.import_tools(server._tool_manager, tool_prefix)+ for key, tool in (await server.get_tools()).items():+ self._tool_manager.add_tool(tool, key=f"{tool_prefix}{key}")# Import resources and templates from the mounted serverresource_prefix = f"{prefix}{resource_separator}"- self._resource_manager.import_resources(- server._resource_manager, resource_prefix- )- self._resource_manager.import_templates(- server._resource_manager, resource_prefix- )+ for key, resource in (await server.get_resources()).items():+ self._resource_manager.add_resource(resource, key=f"{resource_prefix}{key}")+ for key, template in (await server.get_resource_templates()).items():+ self._resource_manager.add_template(template, key=f"{resource_prefix}{key}")# Import prompts from the mounted serverprompt_prefix = f"{prefix}{prompt_separator}"- self._prompt_manager.import_prompts(server._prompt_manager, prompt_prefix)+ for key, prompt in (await server.get_prompts()).items():+ self._prompt_manager.add_prompt(prompt, key=f"{prompt_prefix}{key}")logger.info(f"Imported server {server.name} with prefix '{prefix}'")logger.debug(f"Imported tools with prefix '{tool_prefix}'")@@ -684,37 +815,6 @@ class FastMCP(Generic[LifespanResultT]):logger.debug(f"Imported templates with prefix '{resource_prefix}'")logger.debug(f"Imported prompts with prefix '{prompt_prefix}'")- @classmethod- async def as_proxy(- cls, client: "Client | FastMCP", **settings: Any- ) -> "FastMCPProxy":- """- Create a FastMCP proxy server from a client.-- This method creates a new FastMCP server instance that proxies requests to the provided client.- It discovers the client's tools, resources, prompts, and templates, and creates corresponding- components in the server that forward requests to the client.-- Args:- client: The client to proxy requests to- **settings: Additional settings for the FastMCP server-- Returns:- A FastMCP server that proxies requests to the client- """- from fastmcp.client import Client-- from .proxy import FastMCPProxy-- if isinstance(client, Client):- return await FastMCPProxy.from_client(client=client, **settings)-- elif isinstance(client, FastMCP):- return await FastMCPProxy.from_server(server=client, **settings)-- else:- raise ValueError(f"Unknown client type: {type(client)}")-@classmethoddef from_openapi(cls, openapi_spec: dict[str, Any], client: httpx.AsyncClient, **settings: Any@@ -745,47 +845,11 @@ class FastMCP(Generic[LifespanResultT]):openapi_spec=app.openapi(), client=client, name=name, **settings)+ @classmethod+ def from_client(cls, client: "Client", **settings: Any) -> "FastMCPProxy":+ """+ Create a FastMCP proxy server from a FastMCP client.+ """+ from fastmcp.server.proxy import FastMCPProxy-def _convert_to_content(- result: Any,- _process_as_single_item: bool = False,-) -> list[TextContent | ImageContent | EmbeddedResource]:- """Convert a result to a sequence of content objects."""- if result is None:- return []-- if isinstance(result, TextContent | ImageContent | EmbeddedResource):- return [result]-- if isinstance(result, Image):- return [result.to_image_content()]-- if isinstance(result, list | tuple) and not _process_as_single_item:- # if the result is a list, then it could either be a list of MCP types,- # or a "regular" list that the tool is returning, or a mix of both.- #- # so we extract all the MCP types / images and convert them as individual content elements,- # and aggregate the rest as a single content element-- mcp_types = []- other_content = []-- for item in result:- if isinstance(item, TextContent | ImageContent | EmbeddedResource | Image):- mcp_types.append(_convert_to_content(item)[0])- else:- other_content.append(item)- if other_content:- other_content = _convert_to_content(- other_content, _process_as_single_item=True- )-- return other_content + mcp_types-- if not isinstance(result, str):- try:- result = json.dumps(pydantic_core.to_jsonable_python(result))- except Exception:- result = str(result)-- return [TextContent(type="text", text=result)]+ return FastMCPProxy(client=client, **settings)diff --git a/src/fastmcp/settings.py b/src/fastmcp/settings.pyindex 41b3fcb89..68401f68b 100644--- a/src/fastmcp/settings.py+++ b/src/fastmcp/settings.py@@ -62,6 +62,9 @@ class ServerSettings(BaseSettings):description="List of dependencies to install in the server environment",)+ # cache settings (for checking mounted servers)+ cache_expiration_seconds: float = 5+class ClientSettings(BaseSettings):"""FastMCP client settings."""diff --git a/src/fastmcp/tools/tool.py b/src/fastmcp/tools/tool.pyindex c289150ea..cfdef7692 100644--- a/src/fastmcp/tools/tool.py+++ b/src/fastmcp/tools/tool.py@@ -1,15 +1,18 @@-from __future__ import annotations as _annotations+from __future__ import annotationsimport inspect+import jsonfrom collections.abc import Callablefrom typing import TYPE_CHECKING, Annotated, Any+import pydantic_core+from mcp.types import EmbeddedResource, ImageContent, TextContentfrom mcp.types import Tool as MCPToolfrom pydantic import BaseModel, BeforeValidator, Fieldfrom fastmcp.exceptions import ToolErrorfrom fastmcp.utilities.func_metadata import FuncMetadata, func_metadata-from fastmcp.utilities.types import _convert_set_defaults+from fastmcp.utilities.types import Image, _convert_set_defaultsif TYPE_CHECKING:from mcp.server.session import ServerSessionT@@ -58,7 +61,7 @@ class Tool(BaseModel):is_async = inspect.iscoroutinefunction(fn)if context_kwarg is None:- if isinstance(fn, classmethod):+ if inspect.ismethod(fn) and hasattr(fn, "__func__"):sig = inspect.signature(fn.__func__)else:sig = inspect.signature(fn)@@ -67,14 +70,16 @@ class Tool(BaseModel):context_kwarg = param_namebreak+ # Use callable typing to ensure fn is treated as a callable despite being a classmethod+ fn_callable: Callable[..., Any] = fnfunc_arg_metadata = func_metadata(- fn,+ fn_callable,skip_names=[context_kwarg] if context_kwarg is not None else [],)parameters = func_arg_metadata.arg_model.model_json_schema()return cls(- fn=fn,+ fn=fn_callable,name=func_name,description=func_doc,parameters=parameters,@@ -88,10 +93,10 @@ class Tool(BaseModel):self,arguments: dict[str, Any],context: Context[ServerSessionT, LifespanContextT] | None = None,- ) -> Any:+ ) -> list[TextContent | ImageContent | EmbeddedResource]:"""Run the tool with arguments."""try:- return await self.fn_metadata.call_fn_with_arg_validation(+ result = await self.fn_metadata.call_fn_with_arg_validation(self.fn,self.is_async,arguments,@@ -99,6 +104,7 @@ class Tool(BaseModel):if self.context_kwarg is not Noneelse None,)+ return _convert_to_content(result)except Exception as e:raise ToolError(f"Error executing tool {self.name}: {e}") from e@@ -114,3 +120,48 @@ class Tool(BaseModel):if not isinstance(other, Tool):return Falsereturn self.model_dump() == other.model_dump()+++def _convert_to_content(+ result: Any,+ _process_as_single_item: bool = False,+) -> list[TextContent | ImageContent | EmbeddedResource]:+ """Convert a result to a sequence of content objects."""+ if result is None:+ return []++ if isinstance(result, TextContent | ImageContent | EmbeddedResource):+ return [result]++ if isinstance(result, Image):+ return [result.to_image_content()]++ if isinstance(result, list | tuple) and not _process_as_single_item:+ # if the result is a list, then it could either be a list of MCP types,+ # or a "regular" list that the tool is returning, or a mix of both.+ #+ # so we extract all the MCP types / images and convert them as individual content elements,+ # and aggregate the rest as a single content element++ mcp_types = []+ other_content = []++ for item in result:+ if isinstance(item, TextContent | ImageContent | EmbeddedResource | Image):+ mcp_types.append(_convert_to_content(item)[0])+ else:+ other_content.append(item)+ if other_content:+ other_content = _convert_to_content(+ other_content, _process_as_single_item=True+ )++ return other_content + mcp_types++ if not isinstance(result, str):+ try:+ result = json.dumps(pydantic_core.to_jsonable_python(result))+ except Exception:+ result = str(result)++ return [TextContent(type="text", text=result)]diff --git a/src/fastmcp/tools/tool_manager.py b/src/fastmcp/tools/tool_manager.pyindex f82c9f5be..6da7d0961 100644--- a/src/fastmcp/tools/tool_manager.py+++ b/src/fastmcp/tools/tool_manager.py@@ -4,8 +4,9 @@ from collections.abc import Callablefrom typing import TYPE_CHECKING, Anyfrom mcp.shared.context import LifespanContextT+from mcp.types import EmbeddedResource, ImageContent, TextContent-from fastmcp.exceptions import ToolError+from fastmcp.exceptions import NotFoundErrorfrom fastmcp.settings import DuplicateBehaviorfrom fastmcp.tools.tool import Toolfrom fastmcp.utilities.logging import get_logger@@ -82,29 +83,10 @@ class ToolManager:key: str,arguments: dict[str, Any],context: Context[ServerSessionT, LifespanContextT] | None = None,- ) -> Any:+ ) -> list[TextContent | ImageContent | EmbeddedResource]:"""Call a tool by name with arguments."""tool = self.get_tool(key)if not tool:- raise ToolError(f"Unknown tool: {key}")+ raise NotFoundError(f"Unknown tool: {key}")return await tool.run(arguments, context=context)-- def import_tools(- self, tool_manager: ToolManager, prefix: str | None = None- ) -> None:- """- Import all tools from another ToolManager with prefixed names.-- Args:- tool_manager: Another ToolManager instance to import tools from- prefix: Prefix to add to tool names, including the delimiter.- The resulting tool name will be in the format "{prefix}{original_name}"- if prefix is provided, otherwise the original name is used.- For example, with prefix "weather/" and tool "forecast",- the imported tool would be available as "weather/forecast"- """- for name, tool in tool_manager._tools.items():- key = f"{prefix}{name}" if prefix else name- self.add_tool(tool, key=key)- logger.debug(f'Imported tool "{tool.name}" as "{key}"')diff --git a/tests/client/test_client.py b/tests/client/test_client.pyindex 160b32968..17c299e8c 100644--- a/tests/client/test_client.py+++ b/tests/client/test_client.py@@ -67,36 +67,6 @@ def tagged_resources_server():return server-def mounted_resources_server():- """Fixture that creates a FastMCP server with mounted resources."""- # Create the main server- main_server = FastMCP("MainServer")-- # Create sub-app with its own resources- sub_app = FastMCP("SubAppServer")-- # Add a resource to the sub-app- @sub_app.resource(uri="subapp://data", description="SubApp resource")- async def get_subapp_data():- return {"source": "subapp"}-- # Add a template to the sub-app- @sub_app.resource(uri="subapp://{id}", description="SubApp template")- async def get_subapp_item(id: str):- return {"id": id, "source": "subapp"}-- # Mount the sub-app to the main server with a prefix- main_server.import_server("sub", sub_app)-- # Add a resource to the main server- @main_server.resource(uri="main://data", description="Main resource")- async def get_main_data():- return {"source": "main"}-- return main_server--async def test_list_tools(fastmcp_server):"""Test listing tools with InMemoryClient."""client = Client(transport=FastMCPTransport(fastmcp_server))@@ -296,61 +266,3 @@ async def test_tagged_template_functionality(tagged_resources_server):content_str = str(result[0])assert '"id": "123"' in content_strassert '"type": "template_data"' in content_str---async def test_mounted_resources(mounted_resources_server):- """Test that resources from mounted apps are correctly prefixed."""- client = Client(transport=FastMCPTransport(mounted_resources_server))-- async with client:- resources = await client.list_resources()-- # Should have two resources (one from main, one from sub)- assert len(resources) == 2-- # Find resources by URI- main_resource = next(- (r for r in resources if str(r.uri) == "main://data"), None- )- sub_resource = next(- (r for r in resources if str(r.uri) == "sub+subapp://data"), None- )-- # Both resources should exist- assert main_resource is not None- assert sub_resource is not None-- # Check descriptions- assert main_resource.description == "Main resource"- assert sub_resource.description == "SubApp resource"---async def test_mounted_templates(mounted_resources_server):- """Test that templates from mounted apps are correctly prefixed."""- client = Client(transport=FastMCPTransport(mounted_resources_server))-- async with client:- templates = await client.list_resource_templates()-- # Should have one template (from sub)- assert len(templates) == 1-- # Check the template- template = templates[0]- assert "sub+subapp://{id}" in template.uriTemplate- assert template.description == "SubApp template"---async def test_mounted_template_functionality(mounted_resources_server):- """Test that templates from mounted apps function correctly."""- client = Client(transport=FastMCPTransport(mounted_resources_server))-- async with client:- # Use the prefixed template- uri = cast(AnyUrl, "sub+subapp://123")- result = await client.read_resource(uri)- content_str = str(result[0])-- # Check the content- assert '"id": "123"' in content_str- assert '"source": "subapp"' in content_strdiff --git a/tests/prompts/test_base.py b/tests/prompts/test_base.pyindex 84455d068..24c05ccd8 100644--- a/tests/prompts/test_base.py+++ b/tests/prompts/test_base.py@@ -12,7 +12,6 @@ from fastmcp.prompts.prompt import (class TestRenderPrompt:- @pytest.mark.anyioasync def test_basic_fn(self):def fn() -> str:return "Hello, world!"@@ -22,7 +21,6 @@ class TestRenderPrompt:UserMessage(content=TextContent(type="text", text="Hello, world!"))]- @pytest.mark.anyioasync def test_async_fn(self):async def fn() -> str:return "Hello, world!"@@ -32,7 +30,6 @@ class TestRenderPrompt:UserMessage(content=TextContent(type="text", text="Hello, world!"))]- @pytest.mark.anyioasync def test_fn_with_args(self):async def fn(name: str, age: int = 30) -> str:return f"Hello, {name}! You're {age} years old."@@ -46,7 +43,6 @@ class TestRenderPrompt:)]- @pytest.mark.anyioasync def test_fn_with_invalid_kwargs(self):async def fn(name: str, age: int = 30) -> str:return f"Hello, {name}! You're {age} years old."@@ -55,7 +51,6 @@ class TestRenderPrompt:with pytest.raises(ValueError):await prompt.render(arguments=dict(age=40))- @pytest.mark.anyioasync def test_fn_returns_message(self):async def fn() -> Message:return UserMessage(content="Hello, world!")@@ -65,7 +60,6 @@ class TestRenderPrompt:UserMessage(content=TextContent(type="text", text="Hello, world!"))]- @pytest.mark.anyioasync def test_fn_returns_assistant_message(self):async def fn() -> Message:return AssistantMessage(@@ -77,7 +71,6 @@ class TestRenderPrompt:AssistantMessage(content=TextContent(type="text", text="Hello, world!"))]- @pytest.mark.anyioasync def test_fn_returns_multiple_messages(self):expected = [UserMessage("Hello, world!"),@@ -91,7 +84,6 @@ class TestRenderPrompt:prompt = Prompt.from_function(fn)assert await prompt.render() == expected- @pytest.mark.anyioasync def test_fn_returns_list_of_strings(self):expected = ["Hello, world!",@@ -104,7 +96,6 @@ class TestRenderPrompt:prompt = Prompt.from_function(fn)assert await prompt.render() == [UserMessage(t) for t in expected]- @pytest.mark.anyioasync def test_fn_returns_resource_content(self):"""Test returning a message with resource content."""@@ -134,7 +125,6 @@ class TestRenderPrompt:)]- @pytest.mark.anyioasync def test_fn_returns_mixed_content(self):"""Test returning messages with mixed content types."""@@ -174,7 +164,6 @@ class TestRenderPrompt:),]- @pytest.mark.anyioasync def test_fn_returns_dict_with_resource(self):"""Test returning a dict with resource content."""diff --git a/tests/prompts/test_prompt_manager.py b/tests/prompts/test_prompt_manager.pyindex e53148987..4b6063186 100644--- a/tests/prompts/test_prompt_manager.py+++ b/tests/prompts/test_prompt_manager.py@@ -1,8 +1,8 @@import pytest-from fastmcp.exceptions import PromptError+from fastmcp.exceptions import NotFoundErrorfrom fastmcp.prompts import Prompt-from fastmcp.prompts.prompt import PromptArgument, TextContent, UserMessage+from fastmcp.prompts.prompt import TextContent, UserMessagefrom fastmcp.prompts.prompt_manager import PromptManager@@ -119,8 +119,8 @@ class TestPromptManager:# Result should be the original promptassert result.fn.__name__ == "original_fn"- def test_list_prompts(self):- """Test listing all prompts."""+ def test_get_prompts(self):+ """Test retrieving all prompts."""def fn1() -> str:return "Hello, world!"@@ -133,11 +133,11 @@ class TestPromptManager:prompt2 = Prompt.from_function(fn2)manager.add_prompt(prompt1)manager.add_prompt(prompt2)- prompts = manager.list_prompts()+ prompts = manager.get_prompts()assert len(prompts) == 2- assert prompts == [prompt1, prompt2]+ assert prompts["fn1"] == prompt1+ assert prompts["fn2"] == prompt2- @pytest.mark.anyioasync def test_render_prompt(self):"""Test rendering a prompt."""@@ -152,7 +152,6 @@ class TestPromptManager:UserMessage(content=TextContent(type="text", text="Hello, world!"))]- @pytest.mark.anyioasync def test_render_prompt_with_args(self):"""Test rendering a prompt with arguments."""@@ -167,14 +166,12 @@ class TestPromptManager:UserMessage(content=TextContent(type="text", text="Hello, World!"))]- @pytest.mark.anyioasync def test_render_unknown_prompt(self):"""Test rendering a non-existent prompt."""manager = PromptManager()- with pytest.raises(PromptError, match="Unknown prompt: unknown"):+ with pytest.raises(NotFoundError, match="Unknown prompt: unknown"):await manager.render_prompt("unknown")- @pytest.mark.anyioasync def test_render_prompt_with_missing_args(self):"""Test rendering a prompt with missing required arguments."""@@ -253,132 +250,12 @@ class TestPromptTags:)# Filter prompts by tags- simple_prompts = [p for p in manager.list_prompts() if "simple" in p.tags]+ simple_prompts = [+ p for p in manager.get_prompts().values() if "simple" in p.tags+ ]assert len(simple_prompts) == 2assert {p.name for p in simple_prompts} == {"greeting", "summary"}- nlp_prompts = [p for p in manager.list_prompts() if "nlp" in p.tags]+ nlp_prompts = [p for p in manager.get_prompts().values() if "nlp" in p.tags]assert len(nlp_prompts) == 1assert nlp_prompts[0].name == "summary"-- def test_import_prompts_preserves_tags(self):- """Test that importing prompts preserves their tags."""- source_manager = PromptManager()-- def sample_prompt() -> str:- return "Sample prompt"-- source_manager.add_prompt(- Prompt.from_function(sample_prompt, tags={"example", "test"})- )-- target_manager = PromptManager()- target_manager.import_prompts(source_manager, "imported/")-- imported_prompt = target_manager.get_prompt("imported/sample_prompt")- assert imported_prompt is not None- assert imported_prompt.tags == {"example", "test"}---class TestImports:- def test_import_prompts(self):- """Test importing prompts from one manager to another with a prefix."""- # Setup source manager with prompts- source_manager = PromptManager()-- summary_prompt = Prompt(- name="summary",- description="Generate a summary of text",- arguments=[PromptArgument(name="text", description="Text to summarize")],- fn=lambda: None, # type: ignore- )- source_manager.add_prompt(summary_prompt)-- translate_prompt = Prompt(- name="translate",- description="Translate text to another language",- arguments=[- PromptArgument(name="text", description="Text to translate"),- PromptArgument(name="language", description="Target language"),- ],- fn=lambda: None, # type: ignore- )- source_manager.add_prompt(translate_prompt)-- # Create target manager- target_manager = PromptManager()-- # Import prompts from source to target- prefix = "nlp/"- target_manager.import_prompts(source_manager, prefix)-- # Verify prompts were imported with prefixes- assert "nlp/summary" in target_manager._prompts- assert "nlp/translate" in target_manager._prompts-- # Verify the original prompts still exist in source manager- assert "summary" in source_manager._prompts- assert "translate" in source_manager._prompts-- assert target_manager._prompts["nlp/summary"].fn == summary_prompt.fn- assert target_manager._prompts["nlp/translate"].fn == translate_prompt.fn-- def test_import_prompts_with_duplicates(self):- """Test handling of duplicate prompts during import."""- # Setup source and target managers with same prompt names- source_manager = PromptManager()- target_manager = PromptManager()-- source_prompt = Prompt(- name="common",- description="Source description",- arguments=None,- fn=lambda: None, # type: ignore- )- source_manager._prompts["common"] = source_prompt-- target_prompt = Prompt(- name="common",- description="Target description",- arguments=None,- fn=lambda: None, # type: ignore- )- target_manager._prompts["common"] = target_prompt-- # Import prompts with prefix- prefix = "external/"- target_manager.import_prompts(source_manager, prefix)-- # Verify both prompts exist in target manager- assert "common" in target_manager._prompts- assert "external/common" in target_manager._prompts-- assert target_manager._prompts["common"].fn == target_prompt.fn- assert target_manager._prompts["external/common"].fn == source_prompt.fn-- def test_import_prompts_with_nested_prefixes(self):- """Test importing already prefixed prompts."""- # Setup source manager with already prefixed prompts- first_manager = PromptManager()- second_manager = PromptManager()- third_manager = PromptManager()-- original_prompt = Prompt(- name="analyze",- description="Analyze text",- arguments=[PromptArgument(name="text", description="Text to analyze")],- fn=lambda: None, # type: ignore- )- first_manager._prompts["analyze"] = original_prompt-- # Import to second manager with prefix- second_manager.import_prompts(first_manager, "text/")-- # Import from second to third with another prefix- third_manager.import_prompts(second_manager, "ai/")-- # Verify the nested prefixing- assert "text/analyze" in second_manager._prompts- assert "ai/text/analyze" in third_manager._prompts-- assert third_manager._prompts["ai/text/analyze"].fn == original_prompt.fndiff --git a/tests/resources/test_file_resources.py b/tests/resources/test_file_resources.pyindex ef05e32f3..598e9d88d 100644--- a/tests/resources/test_file_resources.py+++ b/tests/resources/test_file_resources.py@@ -53,7 +53,6 @@ class TestFileResource:assert isinstance(resource.path, Path)assert resource.path.is_absolute()- @pytest.mark.anyioasync def test_read_text_file(self, temp_file: Path):"""Test reading a text file."""resource = FileResource(@@ -65,7 +64,6 @@ class TestFileResource:assert content == "test content"assert resource.mime_type == "text/plain"- @pytest.mark.anyioasync def test_read_binary_file(self, temp_file: Path):"""Test reading a file as binary."""resource = FileResource(@@ -87,7 +85,6 @@ class TestFileResource:path=Path("test.txt"),)- @pytest.mark.anyioasync def test_missing_file_error(self, temp_file: Path):"""Test error when file doesn't exist."""# Create path to non-existent file@@ -103,7 +100,6 @@ class TestFileResource:@pytest.mark.skipif(os.name == "nt", reason="File permissions behave differently on Windows")- @pytest.mark.anyioasync def test_permission_error(self, temp_file: Path):"""Test reading a file without permissions."""temp_file.chmod(0o000) # Remove all permissionsdiff --git a/tests/resources/test_function_resources.py b/tests/resources/test_function_resources.pyindex 5faba0b88..c179fd26c 100644--- a/tests/resources/test_function_resources.py+++ b/tests/resources/test_function_resources.py@@ -25,7 +25,6 @@ class TestFunctionResource:assert resource.mime_type == "text/plain" # defaultassert resource.fn == my_func- @pytest.mark.anyioasync def test_read_text(self):"""Test reading text from a FunctionResource."""@@ -41,7 +40,6 @@ class TestFunctionResource:assert content == "Hello, world!"assert resource.mime_type == "text/plain"- @pytest.mark.anyioasync def test_read_binary(self):"""Test reading binary data from a FunctionResource."""@@ -56,7 +54,6 @@ class TestFunctionResource:content = await resource.read()assert content == b"Hello, world!"- @pytest.mark.anyioasync def test_json_conversion(self):"""Test automatic JSON conversion of non-string results."""@@ -72,7 +69,6 @@ class TestFunctionResource:assert isinstance(content, str)assert '"key": "value"' in content- @pytest.mark.anyioasync def test_error_handling(self):"""Test error handling in FunctionResource."""@@ -87,7 +83,6 @@ class TestFunctionResource:with pytest.raises(ValueError, match="Error reading resource function://test"):await resource.read()- @pytest.mark.anyioasync def test_basemodel_conversion(self):"""Test handling of BaseModel types."""@@ -102,7 +97,6 @@ class TestFunctionResource:content = await resource.read()assert content == '{"name": "test"}'- @pytest.mark.anyioasync def test_custom_type_conversion(self):"""Test handling of custom types."""@@ -121,7 +115,6 @@ class TestFunctionResource:content = await resource.read()assert isinstance(content, str)- @pytest.mark.anyioasync def test_async_read_text(self):"""Test reading text from async FunctionResource."""diff --git a/tests/resources/test_resource_manager.py b/tests/resources/test_resource_manager.pyindex 34f36ff14..293463115 100644--- a/tests/resources/test_resource_manager.py+++ b/tests/resources/test_resource_manager.py@@ -4,7 +4,7 @@ from tempfile import NamedTemporaryFileimport pytestfrom pydantic import AnyUrl, FileUrl-from fastmcp.exceptions import ResourceError+from fastmcp.exceptions import NotFoundErrorfrom fastmcp.resources import (FileResource,FunctionResource,@@ -36,34 +36,41 @@ class TestResourceManager:def test_add_resource(self, temp_file: Path):"""Test adding a resource."""manager = ResourceManager()+ file_url = "file://test-resource"resource = FileResource(- uri=FileUrl(f"file://{temp_file}"),+ uri=FileUrl(file_url),name="test",path=temp_file,)added = manager.add_resource(resource)assert added == resource- assert manager.list_resources() == [resource]+ # Get the actual key from the resource manager+ assert len(manager.get_resources()) == 1+ assert resource in manager.get_resources().values()def test_add_duplicate_resource(self, temp_file: Path):"""Test adding the same resource twice."""manager = ResourceManager()+ file_url = "file://test-resource"resource = FileResource(- uri=FileUrl(f"file://{temp_file}"),+ uri=FileUrl(file_url),name="test",path=temp_file,)first = manager.add_resource(resource)second = manager.add_resource(resource)assert first == second- assert manager.list_resources() == [resource]+ # Check the resource is there+ assert len(manager.get_resources()) == 1+ assert resource in manager.get_resources().values()def test_warn_on_duplicate_resources(self, temp_file: Path, caplog):"""Test warning on duplicate resources."""manager = ResourceManager(duplicate_behavior="warn")+ file_url = "file://test-resource"resource = FileResource(- uri=FileUrl(f"file://{temp_file}"),+ uri=FileUrl(file_url),name="test_resource",path=temp_file,)@@ -73,13 +80,14 @@ class TestResourceManager:assert "Resource already exists" in caplog.text# Should have the resource- assert len(manager.list_resources()) == 1+ assert len(manager.get_resources()) == 1+ assert resource in manager.get_resources().values()def test_disable_warn_on_duplicate_resources(self, temp_file: Path, caplog):"""Test disabling warning on duplicate resources."""manager = ResourceManager(duplicate_behavior="ignore")resource = FileResource(- uri=FileUrl(f"file://{temp_file}"),+ uri=FileUrl(f"file://{temp_file.name}"),name="test",path=temp_file,)@@ -92,7 +100,7 @@ class TestResourceManager:manager = ResourceManager(duplicate_behavior="error")resource = FileResource(- uri=FileUrl(f"file://{temp_file}"),+ uri=FileUrl(f"file://{temp_file.name}"),name="test_resource",path=temp_file,)@@ -106,14 +114,15 @@ class TestResourceManager:"""Test replacing duplicate resources."""manager = ResourceManager(duplicate_behavior="replace")+ file_url = "file://test-resource"resource1 = FileResource(- uri=FileUrl(f"file://{temp_file}"),+ uri=FileUrl(file_url),name="original",path=temp_file,)resource2 = FileResource(- uri=FileUrl(f"file://{temp_file}"),+ uri=FileUrl(file_url),name="replacement",path=temp_file,)@@ -122,7 +131,7 @@ class TestResourceManager:manager.add_resource(resource2)# Should have replaced with the new resource- resources = manager.list_resources()+ resources = list(manager.get_resources().values())assert len(resources) == 1assert resources[0].name == "replacement"@@ -130,14 +139,15 @@ class TestResourceManager:"""Test ignoring duplicate resources."""manager = ResourceManager(duplicate_behavior="ignore")+ file_url = "file://test-resource"resource1 = FileResource(- uri=FileUrl(f"file://{temp_file}"),+ uri=FileUrl(file_url),name="original",path=temp_file,)resource2 = FileResource(- uri=FileUrl(f"file://{temp_file}"),+ uri=FileUrl(file_url),name="replacement",path=temp_file,)@@ -146,7 +156,7 @@ class TestResourceManager:result = manager.add_resource(resource2)# Should keep the original- resources = manager.list_resources()+ resources = list(manager.get_resources().values())assert len(resources) == 1assert resources[0].name == "original"# Result should be the original resource@@ -170,7 +180,7 @@ class TestResourceManager:assert "Template already exists" in caplog.text# Should have the template- assert len(manager.list_templates()) == 1+ assert manager.get_templates() == {"test://{id}": template}def test_error_on_duplicate_templates(self):"""Test error on duplicate templates."""@@ -216,7 +226,7 @@ class TestResourceManager:manager.add_template(template2)# Should have replaced with the new template- templates = manager.list_templates()+ templates = list(manager.get_templates().values())assert len(templates) == 1assert templates[0].name == "replacement"@@ -246,18 +256,17 @@ class TestResourceManager:result = manager.add_template(template2)# Should keep the original- templates = manager.list_templates()+ templates = list(manager.get_templates().values())assert len(templates) == 1assert templates[0].name == "original"# Result should be the original templateassert result.name == "original"- @pytest.mark.anyioasync def test_get_resource(self, temp_file: Path):"""Test getting a resource by URI."""manager = ResourceManager()resource = FileResource(- uri=FileUrl(f"file://{temp_file}"),+ uri=FileUrl(f"file://{temp_file.name}"),name="test",path=temp_file,)@@ -265,7 +274,6 @@ class TestResourceManager:retrieved = await manager.get_resource(resource.uri)assert retrieved == resource- @pytest.mark.anyioasync def test_get_resource_from_template(self):"""Test getting a resource through a template."""manager = ResourceManager()@@ -285,31 +293,34 @@ class TestResourceManager:content = await resource.read()assert content == "Hello, world!"- @pytest.mark.anyioasync def test_get_unknown_resource(self):"""Test getting a non-existent resource."""manager = ResourceManager()- with pytest.raises(ResourceError, match="Unknown resource"):+ with pytest.raises(NotFoundError, match="Unknown resource"):await manager.get_resource(AnyUrl("unknown://test"))- def test_list_resources(self, temp_file: Path):- """Test listing all resources."""+ def test_get_resources(self, temp_file: Path):+ """Test retrieving all resources."""manager = ResourceManager()+ file_url1 = "file://test-resource1"resource1 = FileResource(- uri=FileUrl(f"file://{temp_file}"),+ uri=FileUrl(file_url1),name="test1",path=temp_file,)+ file_url2 = "file://test-resource2"resource2 = FileResource(- uri=FileUrl(f"file://{temp_file}2"),+ uri=FileUrl(file_url2),name="test2",path=temp_file,)manager.add_resource(resource1)manager.add_resource(resource2)- resources = manager.list_resources()+ resources = manager.get_resources()assert len(resources) == 2- assert resources == [resource1, resource2]+ values = list(resources.values())+ assert resource1 in values+ assert resource2 in valuesclass TestResourceTags:@@ -319,7 +330,7 @@ class TestResourceTags:"""Test adding a resource with tags."""manager = ResourceManager()resource = FileResource(- uri=FileUrl(f"file://{temp_file}"),+ uri=FileUrl("file://weather-data"),name="weather_data",path=temp_file,tags={"weather", "data"},@@ -327,7 +338,7 @@ class TestResourceTags:manager.add_resource(resource)# Check that tags are preserved- resources = manager.list_resources()+ resources = list(manager.get_resources().values())assert len(resources) == 1assert resources[0].tags == {"weather", "data"}@@ -348,7 +359,7 @@ class TestResourceTags:)manager.add_resource(resource)- resources = manager.list_resources()+ resources = list(manager.get_resources().values())assert len(resources) == 1assert resources[0].tags == {"sample", "test", "data"}@@ -368,7 +379,7 @@ class TestResourceTags:)manager.add_template(template)- templates = manager.list_templates()+ templates = list(manager.get_templates().values())assert len(templates) == 1assert templates[0].tags == {"users", "template", "data"}@@ -378,7 +389,7 @@ class TestResourceTags:# Create multiple resources with different tagsresource1 = FileResource(- uri=FileUrl(f"file://{temp_file}1"),+ uri=FileUrl("file://weather-data"),name="weather_data",path=temp_file,tags={"weather", "external"},@@ -410,285 +421,17 @@ class TestResourceTags:# Filter resources by tagsinternal_resources = [- r for r in manager.list_resources() if "internal" in r.tags+ r for r in manager.get_resources().values() if "internal" in r.tags]assert len(internal_resources) == 2assert {r.name for r in internal_resources} == {"user_data", "system_data"}external_resources = [- r for r in manager.list_resources() if "external" in r.tags+ r for r in manager.get_resources().values() if "external" in r.tags]assert len(external_resources) == 1assert external_resources[0].name == "weather_data"- def test_import_resources_preserves_tags(self):- """Test that importing resources preserves their tags."""- source_manager = ResourceManager()-- async def get_data():- return "Tagged data"-- resource = FunctionResource(- uri=AnyUrl("data://tagged"),- name="tagged_data",- fn=get_data,- tags={"test", "example", "data"},- )-- source_manager.add_resource(resource)-- target_manager = ResourceManager()- target_manager.import_resources(source_manager, "imported+")-- imported_resources = target_manager.list_resources()- assert len(imported_resources) == 1- assert imported_resources[0].tags == {"test", "example", "data"}-- def test_import_templates_preserves_tags(self):- """Test that importing templates preserves their tags."""- source_manager = ResourceManager()-- def user_template(user_id: str) -> str:- return f"User {user_id}"-- template = ResourceTemplate.from_function(- fn=user_template,- uri_template="users://{user_id}",- name="user_template",- tags={"users", "template", "test"},- )-- source_manager.add_template(template)-- target_manager = ResourceManager()- target_manager.import_templates(source_manager, "imported+")-- imported_templates = target_manager.list_templates()- assert len(imported_templates) == 1- assert imported_templates[0].tags == {"users", "template", "test"}---class TestImports:- def test_import_resources(self):- """Test importing resources from one manager to another with a prefix."""- # Setup source manager with resources- source_manager = ResourceManager()-- # Create mock resource functions- async def weather_fn():- return "Weather data"-- async def traffic_fn():- return "Traffic data"-- # Add resources to source manager- weather_resource = FunctionResource(- uri=AnyUrl("weather://forecast"),- name="weather_forecast",- description="Get weather forecast",- mime_type="application/json",- fn=weather_fn,- )- source_manager._resources["weather://forecast"] = weather_resource-- traffic_resource = FunctionResource(- uri=AnyUrl("traffic://status"),- name="traffic_status",- description="Get traffic status",- mime_type="application/json",- fn=traffic_fn,- )- source_manager._resources["traffic://status"] = traffic_resource-- # Create target manager- target_manager = ResourceManager()-- # Import resources from source to target- prefix = "data+"- target_manager.import_resources(source_manager, prefix)-- # Verify resources were imported with prefixes- assert "data+weather://forecast" in target_manager._resources- assert "data+traffic://status" in target_manager._resources-- # Verify the original resources still exist in source manager- assert "weather://forecast" in source_manager._resources- assert "traffic://status" in source_manager._resources-- # Verify the imported resources have the correct properties- assert (- target_manager._resources["data+weather://forecast"].name- == "weather_forecast"- )- assert (- target_manager._resources["data+weather://forecast"].description- == "Get weather forecast"- )- assert (- target_manager._resources["data+weather://forecast"].mime_type- == "application/json"- )-- assert (- target_manager._resources["data+traffic://status"].name == "traffic_status"- )- assert (- target_manager._resources["data+traffic://status"].description- == "Get traffic status"- )- assert (- target_manager._resources["data+traffic://status"].mime_type- == "application/json"- )-- # Since we're dealing with FunctionResource type, we can safely check function attributes- assert isinstance(- target_manager._resources["data+weather://forecast"], FunctionResource- )- assert isinstance(- target_manager._resources["data+traffic://status"], FunctionResource- )-- weather_resource = target_manager._resources["data+weather://forecast"]- traffic_resource = target_manager._resources["data+traffic://status"]-- if hasattr(weather_resource, "fn") and hasattr(traffic_resource, "fn"):- assert weather_resource.fn.__name__ == weather_fn.__name__- assert traffic_resource.fn.__name__ == traffic_fn.__name__-- def test_import_templates(self):- """Test importing resource templates from one manager to another with a prefix."""- # Setup source manager with templates- source_manager = ResourceManager()-- # Create mock template functions- async def user_fn(**params):- return f"User data for id {params.get('id')}"-- async def product_fn(**params):- return f"Product data for id {params.get('id')}"-- # Add templates to source manager- user_template = ResourceTemplate(- uri_template="api://users/{id}",- name="user_template",- description="Get user by ID",- mime_type="application/json",- fn=user_fn,- parameters={"id": {"type": "string", "description": "User ID"}},- )- source_manager._templates["api://users/{id}"] = user_template-- product_template = ResourceTemplate(- uri_template="api://products/{id}",- name="product_template",- description="Get product by ID",- mime_type="application/json",- fn=product_fn,- parameters={"id": {"type": "string", "description": "Product ID"}},- )- source_manager._templates["api://products/{id}"] = product_template-- # Create target manager- target_manager = ResourceManager()-- # Import templates from source to target- prefix = "shop+"- target_manager.import_templates(source_manager, prefix)-- # Verify templates were imported with prefixes- assert "shop+api://users/{id}" in target_manager._templates- assert "shop+api://products/{id}" in target_manager._templates-- # Verify the original templates still exist in source manager- assert "api://users/{id}" in source_manager._templates- assert "api://products/{id}" in source_manager._templates-- # Verify the imported templates have the correct properties- assert (- target_manager._templates["shop+api://users/{id}"].name == "user_template"- )- assert (- target_manager._templates["shop+api://users/{id}"].description- == "Get user by ID"- )- assert (- target_manager._templates["shop+api://users/{id}"].mime_type- == "application/json"- )- assert target_manager._templates["shop+api://users/{id}"].parameters == {- "id": {"type": "string", "description": "User ID"}- }-- assert (- target_manager._templates["shop+api://products/{id}"].name- == "product_template"- )- assert (- target_manager._templates["shop+api://products/{id}"].description- == "Get product by ID"- )- assert (- target_manager._templates["shop+api://products/{id}"].mime_type- == "application/json"- )- assert target_manager._templates["shop+api://products/{id}"].parameters == {- "id": {"type": "string", "description": "Product ID"}- }-- # Verify the template functions were properly copied (only if the fn attribute exists)- user_template = target_manager._templates["shop+api://users/{id}"]- product_template = target_manager._templates["shop+api://products/{id}"]-- if hasattr(user_template, "fn") and hasattr(product_template, "fn"):- assert user_template.fn.__name__ == user_fn.__name__- assert product_template.fn.__name__ == product_fn.__name__-- def test_import_multiple_resource_types(self):- """Test importing both resources and templates with the same prefix."""- # Setup source manager with both resources and templates- source_manager = ResourceManager()-- # Create mock functions- async def resource_fn():- return "Resource data"-- async def template_fn(**params):- return f"Template data for id {params.get('id')}"-- # Add a resource to source manager- resource = FunctionResource(- uri=AnyUrl("data://resource"),- name="test_resource",- description="Test resource",- mime_type="application/json",- fn=resource_fn,- )- source_manager._resources["data://resource"] = resource-- # Add a template to source manager- template = ResourceTemplate(- uri_template="data://template/{id}",- name="test_template",- description="Test template",- mime_type="application/json",- fn=template_fn,- parameters={"id": {"type": "string", "description": "ID parameter"}},- )- source_manager._templates["data://template/{id}"] = template-- # Create target manager- target_manager = ResourceManager()-- # Import both resources and templates- prefix = "test+"- target_manager.import_resources(source_manager, prefix)- target_manager.import_templates(source_manager, prefix)-- # Verify both resource types were imported with prefixes- assert "test+data://resource" in target_manager._resources- assert "test+data://template/{id}" in target_manager._templates-class TestCustomResourceKeys:"""Test adding resources and templates with custom keys."""@@ -743,7 +486,6 @@ class TestCustomResourceKeys:# The template's internal URI template remains unchangedassert str(manager._templates[custom_key].uri_template) == original_uri_template- @pytest.mark.anyioasync def test_get_resource_with_custom_key(self, temp_file: Path):"""Test that get_resource works with resources added with custom keys."""manager = ResourceManager()@@ -768,10 +510,9 @@ class TestCustomResourceKeys:assert str(retrieved.uri) == original_uri# Should NOT be retrievable by the original URI- with pytest.raises(ResourceError, match="Unknown resource"):+ with pytest.raises(NotFoundError, match="Unknown resource"):await manager.get_resource(original_uri)- @pytest.mark.anyioasync def test_get_resource_from_template_with_custom_key(self):"""Test that templates with custom keys can create resources."""manager = ResourceManager()@@ -797,63 +538,5 @@ class TestCustomResourceKeys:assert content == "Hello, world!"# Shouldn't work with the original template pattern- with pytest.raises(ResourceError, match="Unknown resource"):+ with pytest.raises(NotFoundError, match="Unknown resource"):await manager.get_resource("greet://world")-- def test_import_resources_with_custom_keys(self):- """Test that import_resources properly uses custom keys."""- source_manager = ResourceManager()- target_manager = ResourceManager()-- # Add a resource to source manager- async def resource_fn():- return "Resource data"-- original_uri = "data://original"- resource = FunctionResource(- uri=AnyUrl(original_uri),- name="original_resource",- fn=resource_fn,- )- source_manager.add_resource(resource)-- # Import with prefix which creates a new key- prefix = "imported+"- target_manager.import_resources(source_manager, prefix)-- # Resource should be in target manager with prefixed URI as key- prefixed_uri = f"{prefix}{original_uri}"- assert prefixed_uri in target_manager._resources-- # The resource's internal URI should remain unchanged- stored_resource = target_manager._resources[prefixed_uri]- assert str(stored_resource.uri) == original_uri-- def test_import_templates_with_custom_keys(self):- """Test that import_templates properly uses custom keys."""- source_manager = ResourceManager()- target_manager = ResourceManager()-- # Add a template to source manager- async def template_fn(id: str):- return f"Template {id}"-- original_template = "template://{id}"- template = ResourceTemplate.from_function(- fn=template_fn,- uri_template=original_template,- name="original_template",- )- source_manager.add_template(template)-- # Import with prefix which creates a new key- prefix = "imported+"- target_manager.import_templates(source_manager, prefix)-- # Template should be in target manager with prefixed URI template as key- prefixed_template = f"{prefix}{original_template}"- assert prefixed_template in target_manager._templates-- # The template's internal URI template should remain unchanged- stored_template = target_manager._templates[prefixed_template]- assert str(stored_template.uri_template) == original_templatediff --git a/tests/resources/test_resource_template.py b/tests/resources/test_resource_template.pyindex 301a48297..a8b01c11c 100644--- a/tests/resources/test_resource_template.py+++ b/tests/resources/test_resource_template.py@@ -162,7 +162,6 @@ class TestResourceTemplate:name="test",)- @pytest.mark.anyioasync def test_create_resource(self):"""Test creating a resource from a template."""@@ -186,7 +185,6 @@ class TestResourceTemplate:data = json.loads(content)assert data == {"key": "foo", "value": 123}- @pytest.mark.anyioasync def test_template_error(self):"""Test error handling in template resource creation."""@@ -202,7 +200,6 @@ class TestResourceTemplate:with pytest.raises(ValueError, match="Error creating resource from template"):await template.create_resource("fail://test", {"x": "test"})- @pytest.mark.anyioasync def test_async_text_resource(self):"""Test creating a text resource from async function."""@@ -224,7 +221,6 @@ class TestResourceTemplate:content = await resource.read()assert content == "Hello, world!"- @pytest.mark.anyioasync def test_async_binary_resource(self):"""Test creating a binary resource from async function."""@@ -246,7 +242,6 @@ class TestResourceTemplate:content = await resource.read()assert content == b"test"- @pytest.mark.anyioasync def test_basemodel_conversion(self):"""Test handling of BaseModel types."""@@ -274,7 +269,6 @@ class TestResourceTemplate:data = json.loads(content)assert data == {"key": "foo", "value": 123}- @pytest.mark.anyioasync def test_custom_type_conversion(self):"""Test handling of custom types."""diff --git a/tests/resources/test_resources.py b/tests/resources/test_resources.pyindex 870002c35..9eb3d3721 100644--- a/tests/resources/test_resources.py+++ b/tests/resources/test_resources.py@@ -90,7 +90,6 @@ class TestResourceValidation:)assert resource.mime_type == "application/json"- @pytest.mark.anyioasync def test_resource_read_abstract(self):"""Test that Resource.read() is abstract."""diff --git a/tests/server/test_file_server.py b/tests/server/test_file_server.pyindex 6971c5ebd..b483ac110 100644--- a/tests/server/test_file_server.py+++ b/tests/server/test_file_server.py@@ -73,7 +73,6 @@ def tools(mcp: FastMCP, test_dir: Path) -> FastMCP:return mcpasync def test_list_resources(mcp: FastMCP):resources = await mcp._mcp_list_resources()assert len(resources) == 4@@ -86,7 +85,6 @@ async def test_list_resources(mcp: FastMCP):]async def test_read_resource_dir(mcp: FastMCP):res_iter = await mcp._mcp_read_resource("dir://test_dir")res_list = list(res_iter)@@ -103,7 +101,6 @@ async def test_read_resource_dir(mcp: FastMCP):]async def test_read_resource_file(mcp: FastMCP):res_iter = await mcp._mcp_read_resource("file://test_dir/example.py")res_list = list(res_iter)@@ -112,17 +109,15 @@ async def test_read_resource_file(mcp: FastMCP):assert res.content == "print('hello world')"async def test_delete_file(mcp: FastMCP, test_dir: Path):- await mcp.call_tool(+ await mcp._mcp_call_tool("delete_file", arguments=dict(path=str(test_dir / "example.py")))assert not (test_dir / "example.py").exists()async def test_delete_file_and_check_resources(mcp: FastMCP, test_dir: Path):- await mcp.call_tool(+ await mcp._mcp_call_tool("delete_file", arguments=dict(path=str(test_dir / "example.py")))res_iter = await mcp._mcp_read_resource("file://test_dir/example.py")diff --git a/tests/server/test_import_server.py b/tests/server/test_import_server.pyindex 26e230c84..4583d4406 100644--- a/tests/server/test_import_server.py+++ b/tests/server/test_import_server.py@@ -1,15 +1,14 @@-import contextlibimport jsonfrom urllib.parse import quote-import pytest-from mcp.types import TextContent+from mcp.types import TextContent, TextResourceContents+from fastmcp.client.client import Clientfrom fastmcp.server.server import FastMCP-async def test_mount_basic_functionality():- """Test that the mount method properly imports tools and other resources."""+async def test_import_basic_functionality():+ """Test that the import method properly imports tools and other resources."""# Create main app and sub-appmain_app = FastMCP("MainApp")sub_app = FastMCP("SubApp")@@ -19,8 +18,8 @@ async def test_mount_basic_functionality():def sub_tool() -> str:return "This is from the sub app"- # Mount the sub-app to the main app- main_app.import_server("sub", sub_app)+ # Import the sub-app to the main app+ await main_app.import_server("sub", sub_app)# Verify the tool was imported with the prefixassert "sub_sub_tool" in main_app._tool_manager._tools@@ -33,8 +32,8 @@ async def test_mount_basic_functionality():assert callable(tool.fn)-async def test_mount_multiple_apps():- """Test mounting multiple apps to a main app."""+async def test_import_multiple_apps():+ """Test importing multiple apps to a main app."""# Create main app and multiple sub-appsmain_app = FastMCP("MainApp")weather_app = FastMCP("WeatherApp")@@ -49,17 +48,17 @@ async def test_mount_multiple_apps():def get_headlines() -> str:return "News headlines"- # Mount both sub-apps to the main app- main_app.import_server("weather", weather_app)- main_app.import_server("news", news_app)+ # Import both sub-apps to the main app+ await main_app.import_server("weather", weather_app)+ await main_app.import_server("news", news_app)# Verify tools were imported with the correct prefixesassert "weather_get_forecast" in main_app._tool_manager._toolsassert "news_get_headlines" in main_app._tool_manager._tools-async def test_mount_combines_tools():- """Test that mounting preserves existing tools with the same prefix."""+async def test_import_combines_tools():+ """Test that importing preserves existing tools with the same prefix."""# Create appsmain_app = FastMCP("MainApp")first_app = FastMCP("FirstApp")@@ -74,22 +73,22 @@ async def test_mount_combines_tools():def second_tool() -> str:return "Second app tool"- # Mount first app- main_app.import_server("api", first_app)+ # Import first app+ await main_app.import_server("api", first_app)assert "api_first_tool" in main_app._tool_manager._tools- # Mount second app to same prefix- main_app.import_server("api", second_app)+ # Import second app to same prefix+ await main_app.import_server("api", second_app)# Verify second tool is thereassert "api_second_tool" in main_app._tool_manager._tools- # Tools from both mounts are combined+ # Tools from both imports are combinedassert "api_first_tool" in main_app._tool_manager._tools-async def test_mount_with_resources():- """Test mounting with resources."""+async def test_import_with_resources():+ """Test importing with resources."""# Create appsmain_app = FastMCP("MainApp")data_app = FastMCP("DataApp")@@ -99,15 +98,15 @@ async def test_mount_with_resources():async def get_users():return ["user1", "user2"]- # Mount the data app- main_app.import_server("data", data_app)+ # Import the data app+ await main_app.import_server("data", data_app)# Verify the resource was imported with the prefixassert "data+data://users" in main_app._resource_manager._resources-async def test_mount_with_resource_templates():- """Test mounting with resource templates."""+async def test_import_with_resource_templates():+ """Test importing with resource templates."""# Create appsmain_app = FastMCP("MainApp")user_app = FastMCP("UserApp")@@ -117,15 +116,15 @@ async def test_mount_with_resource_templates():def get_user_profile(user_id: str) -> dict:return {"id": user_id, "name": f"User {user_id}"}- # Mount the user app- main_app.import_server("api", user_app)+ # Import the user app+ await main_app.import_server("api", user_app)# Verify the template was imported with the prefixassert "api+users://{user_id}/profile" in main_app._resource_manager._templates-async def test_mount_with_prompts():- """Test mounting with prompts."""+async def test_import_with_prompts():+ """Test importing with prompts."""# Create appsmain_app = FastMCP("MainApp")assistant_app = FastMCP("AssistantApp")@@ -135,15 +134,15 @@ async def test_mount_with_prompts():def greeting(name: str) -> str:return f"Hello, {name}!"- # Mount the assistant app- main_app.import_server("assistant", assistant_app)+ # Import the assistant app+ await main_app.import_server("assistant", assistant_app)# Verify the prompt was imported with the prefixassert "assistant_greeting" in main_app._prompt_manager._prompts-async def test_mount_multiple_resource_templates():- """Test mounting multiple apps with resource templates."""+async def test_import_multiple_resource_templates():+ """Test importing multiple apps with resource templates."""# Create appsmain_app = FastMCP("MainApp")weather_app = FastMCP("WeatherApp")@@ -158,17 +157,17 @@ async def test_mount_multiple_resource_templates():def get_news(category: str) -> str:return f"News for {category}"- # Mount both apps- main_app.import_server("data", weather_app)- main_app.import_server("content", news_app)+ # Import both apps+ await main_app.import_server("data", weather_app)+ await main_app.import_server("content", news_app)# Verify templates were imported with correct prefixesassert "data+weather://{city}" in main_app._resource_manager._templatesassert "content+news://{category}" in main_app._resource_manager._templates-async def test_mount_multiple_prompts():- """Test mounting multiple apps with prompts."""+async def test_import_multiple_prompts():+ """Test importing multiple apps with prompts."""# Create appsmain_app = FastMCP("MainApp")python_app = FastMCP("PythonApp")@@ -183,61 +182,17 @@ async def test_mount_multiple_prompts():def explain_sql(query: str) -> str:return f"Explaining SQL query:\n{query}"- # Mount both apps- main_app.import_server("python", python_app)- main_app.import_server("sql", sql_app)+ # Import both apps+ await main_app.import_server("python", python_app)+ await main_app.import_server("sql", sql_app)# Verify prompts were imported with correct prefixesassert "python_review_python" in main_app._prompt_manager._promptsassert "sql_explain_sql" in main_app._prompt_manager._prompts[email protected](reason="Lifespans are not yet implemented")-async def test_mount_lifespan():- """Test that the lifespan of a mounted app is properly handled."""- # Create apps-- lifespan_checkpoints = []-- @contextlib.asynccontextmanager- async def lifespan(app: FastMCP):- lifespan_checkpoints.append(f"enter {app.name}")- try:- yield- finally:- lifespan_checkpoints.append(f"exit {app.name}")-- main_app = FastMCP("MainApp", lifespan=lifespan)- sub_app = FastMCP("SubApp", lifespan=lifespan)- sub_app_2 = FastMCP("SubApp2", lifespan=lifespan)-- main_app.import_server("sub", sub_app)- main_app.import_server("sub2", sub_app_2)-- low_level_server = main_app._mcp_server- async with contextlib.AsyncExitStack() as stack:- # Note: this imitates the way that lifespans are entered for mounted- # apps It is presently difficult to stop a running server- # programmatically without error in order to test the exit conditions,- # so this is the next best thing- await stack.enter_async_context(low_level_server.lifespan(low_level_server))- assert lifespan_checkpoints == [- "enter MainApp",- "enter SubApp",- "enter SubApp2",- ]- assert lifespan_checkpoints == [- "enter MainApp",- "enter SubApp",- "enter SubApp2",- "exit SubApp2",- "exit SubApp",- "exit MainApp",- ]---async def test_tool_custom_name_preserved_when_mounted():- """Test that a tool's custom name is preserved when mounted."""+async def test_tool_custom_name_preserved_when_imported():+ """Test that a tool's custom name is preserved when imported."""main_app = FastMCP("MainApp")api_app = FastMCP("APIApp")@@ -245,7 +200,7 @@ async def test_tool_custom_name_preserved_when_mounted():return f"Data for query: {query}"api_app.add_tool(fetch_data, name="get_data")- main_app.import_server("api", api_app)+ await main_app.import_server("api", api_app)# Check that the tool is accessible by its prefixed nametool = main_app._tool_manager.get_tool("api_get_data")@@ -255,8 +210,8 @@ async def test_tool_custom_name_preserved_when_mounted():assert tool.fn.__name__ == "fetch_data"-async def test_call_mounted_custom_named_tool():- """Test calling a mounted tool with a custom name."""+async def test_call_imported_custom_named_tool():+ """Test calling an imported tool with a custom name."""main_app = FastMCP("MainApp")api_app = FastMCP("APIApp")@@ -264,17 +219,18 @@ async def test_call_mounted_custom_named_tool():return f"Data for query: {query}"api_app.add_tool(fetch_data, name="get_data")- main_app.import_server("api", api_app)+ await main_app.import_server("api", api_app)context = main_app.get_context()result = await main_app._tool_manager.call_tool("api_get_data", {"query": "test"}, context=context)- assert result == "Data for query: test"+ assert isinstance(result[0], TextContent)+ assert result[0].text == "Data for query: test"-async def test_first_level_mounting_with_custom_name():- """Test that a tool with a custom name is correctly mounted at the first level."""+async def test_first_level_importing_with_custom_name():+ """Test that a tool with a custom name is correctly imported at the first level."""service_app = FastMCP("ServiceApp")provider_app = FastMCP("ProviderApp")@@ -282,7 +238,7 @@ async def test_first_level_mounting_with_custom_name():return input * 2provider_app.add_tool(calculate_value, name="compute")- service_app.import_server("provider", provider_app)+ await service_app.import_server("provider", provider_app)# Tool is accessible in the service app with the first prefixtool = service_app._tool_manager.get_tool("provider_compute")@@ -290,8 +246,8 @@ async def test_first_level_mounting_with_custom_name():assert tool.fn.__name__ == "calculate_value"-async def test_nested_mounting_preserves_prefixes():- """Test that mounting a previously mounted app preserves prefixes."""+async def test_nested_importing_preserves_prefixes():+ """Test that importing a previously imported app preserves prefixes."""main_app = FastMCP("MainApp")service_app = FastMCP("ServiceApp")provider_app = FastMCP("ProviderApp")@@ -300,16 +256,16 @@ async def test_nested_mounting_preserves_prefixes():return input * 2provider_app.add_tool(calculate_value, name="compute")- service_app.import_server("provider", provider_app)- main_app.import_server("service", service_app)+ await service_app.import_server("provider", provider_app)+ await main_app.import_server("service", service_app)# Tool is accessible in the main app with both prefixestool = main_app._tool_manager.get_tool("service_provider_compute")assert tool is not None-async def test_call_nested_mounted_tool():- """Test calling a tool through multiple levels of mounting."""+async def test_call_nested_imported_tool():+ """Test calling a tool through multiple levels of importing."""main_app = FastMCP("MainApp")service_app = FastMCP("ServiceApp")provider_app = FastMCP("ProviderApp")@@ -318,18 +274,19 @@ async def test_call_nested_mounted_tool():return input * 2provider_app.add_tool(calculate_value, name="compute")- service_app.import_server("provider", provider_app)- main_app.import_server("service", service_app)+ await service_app.import_server("provider", provider_app)+ await main_app.import_server("service", service_app)result = await main_app._tool_manager.call_tool("service_provider_compute", {"input": 21})- assert result == 42+ assert isinstance(result[0], TextContent)+ assert result[0].text == "42"-async def test_mount_with_proxy_tools():+async def test_import_with_proxy_tools():"""- Test mounting with tools that have custom names (proxy tools).+ Test importing with tools that have custom names (proxy tools).This tests that the tool's name doesn't change even though the registeredname does, which is important because we need to forward that name to the@@ -343,16 +300,17 @@ async def test_mount_with_proxy_tools():def get_data(query: str) -> str:return f"Data for query: {query}"- main_app.import_server("api", await FastMCP.as_proxy(api_app))+ proxy_app = FastMCP.from_client(Client(api_app))+ await main_app.import_server("api", proxy_app)- result = await main_app.call_tool("api_get_data", {"query": "test"})+ result = await main_app._mcp_call_tool("api_get_data", {"query": "test"})assert isinstance(result[0], TextContent)assert result[0].text == "Data for query: test"-async def test_mount_with_proxy_prompts():+async def test_import_with_proxy_prompts():"""- Test mounting with prompts that have custom keys.+ Test importing with prompts that have custom keys.This tests that the prompt's name doesn't change even though the registeredkey does, which is important for correct rendering.@@ -365,17 +323,17 @@ async def test_mount_with_proxy_prompts():def greeting(name: str) -> str:return f"Hello, {name} from API!"- main_app.import_server("api", await FastMCP.as_proxy(api_app))+ proxy_app = FastMCP.from_client(Client(api_app))+ await main_app.import_server("api", proxy_app)- result = await main_app.get_prompt("api_greeting", {"name": "World"})- assert len(result) > 0- assert isinstance(result[0].content, TextContent)- assert result[0].content.text == "Hello, World from API!"+ result = await main_app._mcp_get_prompt("api_greeting", {"name": "World"})+ assert result.messages is not None+ # Check that the message contains our greeting-async def test_mount_with_proxy_resources():+async def test_import_with_proxy_resources():"""- Test mounting with resources that have custom keys.+ Test importing with resources that have custom keys.This tests that the resource's name doesn't change even though the registeredkey does, which is important for correct access.@@ -392,19 +350,21 @@ async def test_mount_with_proxy_resources():"base_url": "https://api.example.com",}- main_app.import_server("api", await FastMCP.as_proxy(api_app))+ proxy_app = FastMCP.from_client(Client(api_app))+ await main_app.import_server("api", proxy_app)# Access the resource through the main app with the prefixed key- resource = await main_app.read_resource("api+config://settings")- assert resource is not None- resource = json.loads(resource)- assert resource["api_key"] == "12345"- assert resource["base_url"] == "https://api.example.com"+ async with Client(main_app) as client:+ result = await client.read_resource("api+config://settings")+ assert isinstance(result[0], TextResourceContents)+ config_data = json.loads(result[0].text)+ assert config_data["api_key"] == "12345"+ assert config_data["base_url"] == "https://api.example.com"-async def test_mount_with_proxy_resource_templates():+async def test_import_with_proxy_resource_templates():"""- Test mounting with resource templates that have custom keys.+ Test importing with resource templates that have custom keys.This tests that the template's name doesn't change even though the registeredkey does, which is important for correct instantiation.@@ -418,13 +378,15 @@ async def test_mount_with_proxy_resource_templates():def create_user(name: str, email: str):return {"name": name, "email": email}- main_app.import_server("api", await FastMCP.as_proxy(api_app))+ proxy_app = FastMCP.from_client(Client(api_app))+ await main_app.import_server("api", proxy_app)# Instantiate the template through the main app with the prefixed keyquoted_name = quote("John Doe", safe="")quoted_email = quote("[email protected]", safe="")- user = await main_app.read_resource(f"api+user://{quoted_name}/{quoted_email}")- assert user is not None- user = json.loads(user)- assert user["name"] == "John Doe"- assert user["email"] == "[email protected]"+ async with Client(main_app) as client:+ result = await client.read_resource(f"api+user://{quoted_name}/{quoted_email}")+ assert isinstance(result[0], TextResourceContents)+ user_data = json.loads(result[0].text)+ assert user_data["name"] == "John Doe"+ assert user_data["email"] == "[email protected]"diff --git a/tests/server/test_lifespan.py b/tests/server/test_lifespan.pyindex 30598d4ad..aa1aef466 100644--- a/tests/server/test_lifespan.py+++ b/tests/server/test_lifespan.py@@ -4,7 +4,6 @@ from collections.abc import AsyncIteratorfrom contextlib import asynccontextmanagerimport anyio-import pytestfrom mcp.types import (ClientCapabilities,Implementation,@@ -18,7 +17,6 @@ from pydantic import TypeAdapterfrom fastmcp import Context, FastMCPasync def test_fastmcp_server_lifespan():"""Test that lifespan works in FastMCP server."""diff --git a/tests/server/test_openapi.py b/tests/server/test_openapi.pyindex 160c29d6c..afad3b669 100644--- a/tests/server/test_openapi.py+++ b/tests/server/test_openapi.py@@ -1,3 +1,4 @@+import jsonimport reimport httpx@@ -5,10 +6,12 @@ import pytestfrom dirty_equals import IsStrfrom fastapi import FastAPI, HTTPExceptionfrom httpx import ASGITransport, AsyncClient+from mcp.types import TextContentfrom pydantic import BaseModel, TypeAdapterfrom pydantic.networks import AnyUrlfrom fastmcp import FastMCP+from fastmcp.client import Clientfrom fastmcp.server.openapi import FastMCPOpenAPI@@ -153,13 +156,19 @@ class TestTools:"""The tool created by the OpenAPI server should be the same as the original"""- tool_response = await fastmcp_openapi_server.call_tool(+ tool_response = await fastmcp_openapi_server._mcp_call_tool("create_user_users_post", {"name": "David", "active": False})- assert tool_response == User(id=4, name="David", active=False)- # Check that the user was created via API+ # Convert TextContent to dict for comparison+ assert isinstance(tool_response, list) and len(tool_response) == 1+ assert isinstance(tool_response[0], TextContent)++ response_data = json.loads(tool_response[0].text)+ expected_user = User(id=4, name="David", active=False).model_dump()+ assert response_data == expected_user+ # Check that the user was created via APIresponse = await api_client.get("/users")assert len(response.json()) == 4@@ -168,7 +177,7 @@ class TestTools:"resource://openapi/get_user_users__user_id__get/4")user = user_response[0].content- assert user == tool_response.model_dump()+ assert user == expected_userasync def test_call_update_user_name_tool(self, fastmcp_openapi_server: FastMCPOpenAPI, api_client@@ -176,21 +185,28 @@ class TestTools:"""The tool created by the OpenAPI server should be the same as the original"""- tool_response = await fastmcp_openapi_server.call_tool(+ tool_response = await fastmcp_openapi_server._mcp_call_tool("update_user_name_users__user_id__name_patch", {"user_id": 1, "name": "XYZ"})- assert tool_response == dict(id=1, name="XYZ", active=True)++ # Convert TextContent to dict for comparison+ assert isinstance(tool_response, list) and len(tool_response) == 1+ assert isinstance(tool_response[0], TextContent)++ response_data = json.loads(tool_response[0].text)+ expected_data = dict(id=1, name="XYZ", active=True)+ assert response_data == expected_data# Check that the user was updated via APIresponse = await api_client.get("/users")- assert dict(id=1, name="XYZ", active=True) in response.json()+ assert expected_data in response.json()# Check that the user was updated via MCPuser_response = await fastmcp_openapi_server._mcp_read_resource("resource://openapi/get_user_users__user_id__get/1")user = user_response[0].content- assert user == tool_response+ assert user == expected_dataclass TestResources:@@ -308,7 +324,9 @@ class TestTagTransfer:):"""Test that tags from OpenAPI routes are correctly transferred to Resources."""# Get internal resources directly- resources = fastmcp_openapi_server._resource_manager.list_resources()+ resources = list(+ fastmcp_openapi_server._resource_manager.get_resources().values()+ )# Find the get_users resourceget_users_resource = next(@@ -327,7 +345,9 @@ class TestTagTransfer:):"""Test that tags from OpenAPI routes are correctly transferred to ResourceTemplates."""# Get internal resource templates directly- templates = fastmcp_openapi_server._resource_manager.list_templates()+ templates = list(+ fastmcp_openapi_server._resource_manager.get_templates().values()+ )# Find the get_user templateget_user_template = next(@@ -346,7 +366,9 @@ class TestTagTransfer:):"""Test that tags are preserved when creating resources from templates."""# Get internal resource templates directly- templates = fastmcp_openapi_server._resource_manager.list_templates()+ templates = list(+ fastmcp_openapi_server._resource_manager.get_templates().values()+ )# Find the get_user templateget_user_template = next(@@ -513,12 +535,17 @@ class TestOpenAPI30Compatibility:async def test_tool_execution(self, openapi_30_server):"""Test executing a tool from an OpenAPI 3.0 server."""- tool_response = await openapi_30_server.call_tool(- "createProduct", {"name": "New Product", "price": 39.99}- )- assert tool_response["id"] == "p3"- assert tool_response["name"] == "New Product"- assert tool_response["price"] == 39.99+ async with Client(openapi_30_server) as client:+ result = await client.call_tool(+ "createProduct", {"name": "New Product", "price": 39.99}+ )+ # Result should be a text content+ assert len(result) == 1+ assert isinstance(result[0], TextContent)+ product = json.loads(result[0].text)+ assert product["id"] == "p3"+ assert product["name"] == "New Product"+ assert product["price"] == 39.99class TestOpenAPI31Compatibility:@@ -679,12 +706,17 @@ class TestOpenAPI31Compatibility:async def test_tool_execution(self, openapi_31_server):"""Test executing a tool from an OpenAPI 3.1 server."""- tool_response = await openapi_31_server.call_tool(- "createOrder", {"customer": "Charlie", "items": ["item4", "item5"]}- )- assert tool_response["id"] == "o3"- assert tool_response["customer"] == "Charlie"- assert tool_response["items"] == ["item4", "item5"]+ async with Client(openapi_31_server) as client:+ result = await client.call_tool(+ "createOrder", {"customer": "Charlie", "items": ["item4", "item5"]}+ )+ # Result should be a text content+ assert len(result) == 1+ assert isinstance(result[0], TextContent)+ order = json.loads(result[0].text)+ assert order["id"] == "o3"+ assert order["customer"] == "Charlie"+ assert order["items"] == ["item4", "item5"]class TestMountFastMCP:@@ -694,7 +726,7 @@ class TestMountFastMCP:"""Test mounting an OpenAPI server."""mcp = FastMCP("MainApp")- mcp.import_server("fastapi", fastmcp_openapi_server)+ await mcp.import_server("fastapi", fastmcp_openapi_server)# Check that resources are available with prefixed URIsresources = await mcp._mcp_list_resources()diff --git a/tests/server/test_proxy.py b/tests/server/test_proxy.pyindex 75e9b7a1f..882977527 100644--- a/tests/server/test_proxy.py+++ b/tests/server/test_proxy.py@@ -1,13 +1,14 @@import jsonfrom typing import Any+import mcp.typesimport pytestfrom dirty_equals import Containsfrom fastmcp import FastMCPfrom fastmcp.client import Clientfrom fastmcp.client.transports import FastMCPTransport-from fastmcp.exceptions import ResourceError+from fastmcp.exceptions import ClientErrorfrom fastmcp.server.proxy import FastMCPProxyUSERS = [@@ -64,7 +65,7 @@ def fastmcp_server():@pytest.fixtureasync def proxy_server(fastmcp_server):"""Fixture that creates a FastMCP proxy server."""- return await FastMCP.as_proxy(Client(transport=FastMCPTransport(fastmcp_server)))+ return FastMCP.from_client(Client(transport=FastMCPTransport(fastmcp_server)))async def test_create_proxy(fastmcp_server):@@ -72,7 +73,7 @@ async def test_create_proxy(fastmcp_server):# Create a clientclient = Client(transport=FastMCPTransport(fastmcp_server))- server = await FastMCPProxy.from_client(client)+ server = FastMCPProxy.from_client(client)assert isinstance(server, FastMCPProxy)assert isinstance(server, FastMCP)@@ -81,7 +82,7 @@ async def test_create_proxy(fastmcp_server):class TestTools:async def test_get_tools(self, proxy_server):- tools = proxy_server.get_tools()+ tools = await proxy_server.get_tools()assert "greet" in toolsassert "add" in toolsassert "error_tool" in tools@@ -95,25 +96,28 @@ class TestTools:async def test_call_tool_result_same_as_original(self, fastmcp_server: FastMCP, proxy_server: FastMCPProxy):- result = await fastmcp_server.call_tool("greet", {"name": "Alice"})- proxy_result = await proxy_server.call_tool("greet", {"name": "Alice"})+ result = await fastmcp_server._mcp_call_tool("greet", {"name": "Alice"})+ proxy_result = await proxy_server._mcp_call_tool("greet", {"name": "Alice"})assert result == proxy_resultasync def test_call_tool_calls_tool(self, proxy_server):- proxy_result = await proxy_server.call_tool("add", {"a": 1, "b": 2})+ async with Client(proxy_server) as client:+ proxy_result = await client.call_tool("add", {"a": 1, "b": 2})+ assert isinstance(proxy_result[0], mcp.types.TextContent)assert proxy_result[0].text == "3"async def test_error_tool_raises_error(self, proxy_server):- with pytest.raises(ValueError, match="This is a test error"):- await proxy_server.call_tool("error_tool", {})+ with pytest.raises(ClientError, match=""):+ async with Client(proxy_server) as client:+ await client.call_tool("error_tool", {})class TestResources:- async def test_list_resources(self, proxy_server):- resources = proxy_server.list_resources()- assert [r.name for r in resources] == Contains(+ async def test_get_resources(self, proxy_server):+ resources = await proxy_server.get_resources()+ assert [r.name for r in resources.values()] == Contains("data://users", "resource://wave")@@ -124,29 +128,36 @@ class TestResources:)async def test_read_resource(self, proxy_server: FastMCPProxy):- result = await proxy_server.read_resource("resource://wave")- assert result == "π"+ async with Client(proxy_server) as client:+ result = await client.read_resource("resource://wave")+ assert isinstance(result[0], mcp.types.TextResourceContents)+ assert result[0].text == "π"async def test_read_resource_same_as_original(self, fastmcp_server, proxy_server):- result = await fastmcp_server.read_resource("resource://wave")- proxy_result = await proxy_server.read_resource("resource://wave")+ async with Client(fastmcp_server) as client:+ result = await client.read_resource("resource://wave")+ async with Client(proxy_server) as client:+ proxy_result = await client.read_resource("resource://wave")assert proxy_result == resultasync def test_read_json_resource(self, proxy_server: FastMCPProxy):- result = await proxy_server.read_resource("data://users")- assert json.loads(result) == USERS+ async with Client(proxy_server) as client:+ result = await client.read_resource("data://users")+ assert isinstance(result[0], mcp.types.TextResourceContents)+ assert json.loads(result[0].text) == USERSasync def test_read_resource_returns_none_if_not_found(self, proxy_server):with pytest.raises(- ResourceError, match="Unknown resource: resource://nonexistent"+ ClientError, match="Unknown resource: resource://nonexistent"):- await proxy_server.read_resource("resource://nonexistent")+ async with Client(proxy_server) as client:+ await client.read_resource("resource://nonexistent")class TestResourceTemplates:- async def test_list_resource_templates(self, proxy_server):- templates = proxy_server.list_resource_templates()- assert [t.name for t in templates] == Contains("get_user")+ async def test_get_resource_templates(self, proxy_server):+ templates = await proxy_server.get_resource_templates()+ assert [t.name for t in templates.values()] == Contains("get_user")async def test_list_resource_templates_same_as_original(self, fastmcp_server, proxy_server@@ -157,35 +168,46 @@ class TestResourceTemplates:@pytest.mark.parametrize("id", [1, 2, 3])async def test_read_resource_template(self, proxy_server: FastMCPProxy, id: int):- result = await proxy_server.read_resource(f"data://user/{id}")- assert json.loads(result) == USERS[id - 1]+ async with Client(proxy_server) as client:+ result = await client.read_resource(f"data://user/{id}")+ assert isinstance(result[0], mcp.types.TextResourceContents)+ assert json.loads(result[0].text) == USERS[id - 1]async def test_read_resource_template_same_as_original(self, fastmcp_server, proxy_server):- result = await fastmcp_server.read_resource("data://user/1")- proxy_result = await proxy_server.read_resource("data://user/1")+ async with Client(fastmcp_server) as client:+ result = await client.read_resource("data://user/1")+ async with Client(proxy_server) as client:+ proxy_result = await client.read_resource("data://user/1")assert proxy_result == resultclass TestPrompts:- async def test_list_prompts(self, proxy_server):- prompts = proxy_server.list_prompts()- assert [p.name for p in prompts] == Contains("welcome")+ async def test_get_prompts_server_method(self, proxy_server: FastMCPProxy):+ prompts = await proxy_server.get_prompts()+ assert [p.name for p in prompts.values()] == Contains("welcome")async def test_list_prompts_same_as_original(self, fastmcp_server, proxy_server):- assert (- await proxy_server._mcp_list_prompts()- == await fastmcp_server._mcp_list_prompts()- )+ async with Client(fastmcp_server) as client:+ result = await client.list_prompts()+ async with Client(proxy_server) as client:+ proxy_result = await client.list_prompts()+ assert proxy_result == resultasync def test_render_prompt_same_as_original(- self, fastmcp_server: FastMCP, proxy_server+ self, fastmcp_server: FastMCP, proxy_server: FastMCPProxy):- result = await fastmcp_server.get_prompt("welcome", {"name": "Alice"})- proxy_result = await proxy_server.get_prompt("welcome", {"name": "Alice"})+ async with Client(fastmcp_server) as client:+ result = await client.get_prompt("welcome", {"name": "Alice"})+ async with Client(proxy_server) as client:+ proxy_result = await client.get_prompt("welcome", {"name": "Alice"})assert proxy_result == resultasync def test_render_prompt_calls_prompt(self, proxy_server):- result = await proxy_server.get_prompt("welcome", {"name": "Alice"})+ async with Client(proxy_server) as client:+ result = await client.get_prompt("welcome", {"name": "Alice"})+ assert isinstance(result[0], mcp.types.PromptMessage)+ assert result[0].role == "user"+ assert isinstance(result[0].content, mcp.types.TextContent)assert result[0].content.text == "Welcome to FastMCP, Alice!"diff --git a/tests/server/test_run_server.py b/tests/server/test_run_server.pyindex fb936b578..5109a27bc 100644--- a/tests/server/test_run_server.py+++ b/tests/server/test_run_server.py@@ -93,6 +93,6 @@# class TestRunServerSSE:-# @pytest.mark.anyio+## async def test_run_server_sse(self, fastmcp_server: FastMCP):# passdiff --git a/tests/server/test_server.py b/tests/server/test_server.pyindex c5cef1fee..42a47ae65 100644--- a/tests/server/test_server.py+++ b/tests/server/test_server.py@@ -4,7 +4,6 @@ from pathlib import Pathfrom typing import TYPE_CHECKINGimport pytest-from mcp.shared.exceptions import McpErrorfrom mcp.types import (BlobResourceContents,ImageContent,@@ -14,7 +13,7 @@ from mcp.types import (from pydantic import AnyUrl, Fieldfrom fastmcp import Client, Context, FastMCP-from fastmcp.exceptions import ResourceError, ToolError+from fastmcp.exceptions import ClientError, NotFoundError, ToolErrorfrom fastmcp.prompts.prompt import EmbeddedResource, Message, UserMessagefrom fastmcp.resources import FileResource, FunctionResourcefrom fastmcp.utilities.types import Image@@ -89,8 +88,8 @@ class TestToolDecorator:async def test_no_tools_before_decorator(self):mcp = FastMCP()- with pytest.raises(ToolError, match="Unknown tool: add"):- await mcp.call_tool("add", {"x": 1, "y": 2})+ with pytest.raises(NotFoundError, match="Unknown tool: add"):+ await mcp._mcp_call_tool("add", {"x": 1, "y": 2})async def test_tool_decorator(self):mcp = FastMCP()@@ -99,7 +98,7 @@ class TestToolDecorator:def add(x: int, y: int) -> int:return x + y- result = await mcp.call_tool("add", {"x": 1, "y": 2})+ result = await mcp._mcp_call_tool("add", {"x": 1, "y": 2})assert isinstance(result[0], TextContent)assert result[0].text == "3"@@ -119,7 +118,7 @@ class TestToolDecorator:def add(x: int, y: int) -> int:return x + y- result = await mcp.call_tool("custom-add", {"x": 1, "y": 2})+ result = await mcp._mcp_call_tool("custom-add", {"x": 1, "y": 2})assert isinstance(result[0], TextContent)assert result[0].text == "3"@@ -148,7 +147,7 @@ class TestToolDecorator:obj = MyClass(10)mcp.add_tool(obj.add)- result = await mcp.call_tool("add", {"y": 2})+ result = await mcp._mcp_call_tool("add", {"y": 2})assert isinstance(result[0], TextContent)assert result[0].text == "12"@@ -163,7 +162,7 @@ class TestToolDecorator:return cls.x + ymcp.add_tool(MyClass.add)- result = await mcp.call_tool("add", {"y": 2})+ result = await mcp._mcp_call_tool("add", {"y": 2})assert isinstance(result[0], TextContent)assert result[0].text == "12"@@ -176,7 +175,7 @@ class TestToolDecorator:def add(x: int, y: int) -> int:return x + y- result = await mcp.call_tool("add", {"x": 1, "y": 2})+ result = await mcp._mcp_call_tool("add", {"x": 1, "y": 2})assert isinstance(result[0], TextContent)assert result[0].text == "3"@@ -187,7 +186,7 @@ class TestToolDecorator:async def add(x: int, y: int) -> int:return x + y- result = await mcp.call_tool("add", {"x": 1, "y": 2})+ result = await mcp._mcp_call_tool("add", {"x": 1, "y": 2})assert isinstance(result[0], TextContent)assert result[0].text == "3"@@ -202,7 +201,7 @@ class TestToolDecorator:return cls.x + ymcp.add_tool(MyClass.add)- result = await mcp.call_tool("add", {"y": 2})+ result = await mcp._mcp_call_tool("add", {"y": 2})assert isinstance(result[0], TextContent)assert result[0].text == "12"@@ -215,7 +214,7 @@ class TestToolDecorator:return x + ymcp.add_tool(MyClass.add)- result = await mcp.call_tool("add", {"x": 1, "y": 2})+ result = await mcp._mcp_call_tool("add", {"x": 1, "y": 2})assert isinstance(result[0], TextContent)assert result[0].text == "3"@@ -240,15 +239,14 @@ class TestToolDecorator:"""Multiply two numbers."""return a * b- # Add the tool with a custom namemcp.add_tool(multiply, name="custom_multiply")# Check that the tool is registered with the custom name- tools = mcp.get_tools()+ tools = await mcp.get_tools()assert "custom_multiply" in tools# Call the tool by its custom name- result = await mcp.call_tool("custom_multiply", {"a": 5, "b": 3})+ result = await mcp._mcp_call_tool("custom_multiply", {"a": 5, "b": 3})assert isinstance(result[0], TextContent)assert result[0].text == "15"@@ -260,8 +258,9 @@ class TestResourceDecorator:async def test_no_resources_before_decorator(self):mcp = FastMCP()- with pytest.raises(ResourceError, match="Unknown resource"):- await mcp.read_resource("resource://data")+ with pytest.raises(ClientError, match="Unknown resource"):+ async with Client(mcp) as client:+ await client.read_resource("resource://data")async def test_resource_decorator(self):mcp = FastMCP()@@ -270,8 +269,10 @@ class TestResourceDecorator:def get_data() -> str:return "Hello, world!"- result = await mcp.read_resource("resource://data")- assert result == "Hello, world!"+ async with Client(mcp) as client:+ result = await client.read_resource("resource://data")+ assert isinstance(result[0], TextResourceContents)+ assert result[0].text == "Hello, world!"async def test_resource_decorator_incorrect_usage(self):mcp = FastMCP()@@ -291,12 +292,15 @@ class TestResourceDecorator:def get_data() -> str:return "Hello, world!"- resources = mcp.list_resources()+ resources_dict = await mcp.get_resources()+ resources = list(resources_dict.values())assert len(resources) == 1assert resources[0].name == "custom-data"- result = await mcp.read_resource("resource://data")- assert result == "Hello, world!"+ async with Client(mcp) as client:+ result = await client.read_resource("resource://data")+ assert isinstance(result[0], TextResourceContents)+ assert result[0].text == "Hello, world!"async def test_resource_decorator_with_description(self):mcp = FastMCP()@@ -305,10 +309,24 @@ class TestResourceDecorator:def get_data() -> str:return "Hello, world!"- resources = mcp.list_resources()+ resources_dict = await mcp.get_resources()+ resources = list(resources_dict.values())assert len(resources) == 1assert resources[0].description == "Data resource"+ async def test_resource_decorator_with_tags(self):+ """Test that the resource decorator properly sets tags."""+ mcp = FastMCP()++ @mcp.resource("resource://data", tags={"example", "test-tag"})+ def get_data() -> str:+ return "Hello, world!"++ resources_dict = await mcp.get_resources()+ resources = list(resources_dict.values())+ assert len(resources) == 1+ assert resources[0].tags == {"example", "test-tag"}+async def test_resource_decorator_instance_method(self):mcp = FastMCP()@@ -324,8 +342,10 @@ class TestResourceDecorator:obj.get_data, uri="resource://data", name="instance-resource")- result = await mcp.read_resource("resource://data")- assert result == "My prefix: Hello, world!"+ async with Client(mcp) as client:+ result = await client.read_resource("resource://data")+ assert isinstance(result[0], TextResourceContents)+ assert result[0].text == "My prefix: Hello, world!"async def test_resource_decorator_classmethod(self):mcp = FastMCP()@@ -341,8 +361,10 @@ class TestResourceDecorator:MyClass.get_data, uri="resource://data", name="class-resource")- result = await mcp.read_resource("resource://data")- assert result == "Class prefix: Hello, world!"+ async with Client(mcp) as client:+ result = await client.read_resource("resource://data")+ assert isinstance(result[0], TextResourceContents)+ assert result[0].text == "Class prefix: Hello, world!"async def test_resource_decorator_staticmethod(self):mcp = FastMCP()@@ -353,8 +375,10 @@ class TestResourceDecorator:def get_data() -> str:return "Static Hello, world!"- result = await mcp.read_resource("resource://data")- assert result == "Static Hello, world!"+ async with Client(mcp) as client:+ result = await client.read_resource("resource://data")+ assert isinstance(result[0], TextResourceContents)+ assert result[0].text == "Static Hello, world!"async def test_resource_decorator_async_function(self):mcp = FastMCP()@@ -363,19 +387,10 @@ class TestResourceDecorator:async def get_data() -> str:return "Async Hello, world!"- result = await mcp.read_resource("resource://data")- assert result == "Async Hello, world!"-- async def test_resource_decorator_with_tags(self):- mcp = FastMCP()-- @mcp.resource("resource://data", tags={"example", "test-tag"})- def get_data() -> str:- return "Hello, world!"-- resources = mcp.list_resources()- assert len(resources) == 1- assert resources[0].tags == {"example", "test-tag"}+ async with Client(mcp) as client:+ result = await client.read_resource("resource://data")+ assert isinstance(result[0], TextResourceContents)+ assert result[0].text == "Async Hello, world!"class TestTemplateDecorator:@@ -386,12 +401,16 @@ class TestTemplateDecorator:def get_data(name: str) -> str:return f"Data for {name}"- templates = mcp.list_resource_templates()+ templates_dict = await mcp.get_resource_templates()+ templates = list(templates_dict.values())assert len(templates) == 1+ assert templates[0].name == "get_data"assert templates[0].uri_template == "resource://{name}/data"- result = await mcp.read_resource("resource://test/data")- assert result == "Data for test"+ async with Client(mcp) as client:+ result = await client.read_resource("resource://test/data")+ assert isinstance(result[0], TextResourceContents)+ assert result[0].text == "Data for test"async def test_template_decorator_incorrect_usage(self):mcp = FastMCP()@@ -411,12 +430,15 @@ class TestTemplateDecorator:def get_data(name: str) -> str:return f"Data for {name}"- templates = mcp.list_resource_templates()+ templates_dict = await mcp.get_resource_templates()+ templates = list(templates_dict.values())assert len(templates) == 1assert templates[0].name == "custom-template"- result = await mcp.read_resource("resource://test/data")- assert result == "Data for test"+ async with Client(mcp) as client:+ result = await client.read_resource("resource://test/data")+ assert isinstance(result[0], TextResourceContents)+ assert result[0].text == "Data for test"async def test_template_decorator_with_description(self):mcp = FastMCP()@@ -425,7 +447,8 @@ class TestTemplateDecorator:def get_data(name: str) -> str:return f"Data for {name}"- templates = mcp.list_resource_templates()+ templates_dict = await mcp.get_resource_templates()+ templates = list(templates_dict.values())assert len(templates) == 1assert templates[0].description == "Template description"@@ -440,13 +463,14 @@ class TestTemplateDecorator:return f"{self.prefix} Data for {name}"obj = MyClass("My prefix:")-mcp.add_resource_fn(obj.get_data, uri="resource://{name}/data", name="instance-template")- result = await mcp.read_resource("resource://test/data")- assert result == "My prefix: Data for test"+ async with Client(mcp) as client:+ result = await client.read_resource("resource://test/data")+ assert isinstance(result[0], TextResourceContents)+ assert result[0].text == "My prefix: Data for test"async def test_template_decorator_classmethod(self):mcp = FastMCP()@@ -459,11 +483,15 @@ class TestTemplateDecorator:return f"{cls.prefix} Data for {name}"mcp.add_resource_fn(- MyClass.get_data, uri="resource://{name}/data", name="class-template"+ MyClass.get_data,+ uri="resource://{name}/data",+ name="class-template",)- result = await mcp.read_resource("resource://test/data")- assert result == "Class prefix: Data for test"+ async with Client(mcp) as client:+ result = await client.read_resource("resource://test/data")+ assert isinstance(result[0], TextResourceContents)+ assert result[0].text == "Class prefix: Data for test"async def test_template_decorator_staticmethod(self):mcp = FastMCP()@@ -474,8 +502,10 @@ class TestTemplateDecorator:def get_data(name: str) -> str:return f"Static Data for {name}"- result = await mcp.read_resource("resource://test/data")- assert result == "Static Data for test"+ async with Client(mcp) as client:+ result = await client.read_resource("resource://test/data")+ assert isinstance(result[0], TextResourceContents)+ assert result[0].text == "Static Data for test"async def test_template_decorator_async_function(self):mcp = FastMCP()@@ -484,19 +514,22 @@ class TestTemplateDecorator:async def get_data(name: str) -> str:return f"Async Data for {name}"- result = await mcp.read_resource("resource://test/data")- assert result == "Async Data for test"+ async with Client(mcp) as client:+ result = await client.read_resource("resource://test/data")+ assert isinstance(result[0], TextResourceContents)+ assert result[0].text == "Async Data for test"async def test_template_decorator_with_tags(self):+ """Test that the template decorator properly sets tags."""mcp = FastMCP()- @mcp.resource("resource://{name}/data", tags={"template", "test-tag"})- def get_data(name: str) -> str:- return f"Data for {name}"+ @mcp.resource("resource://{param}", tags={"template", "test-tag"})+ def template_resource(param: str) -> str:+ return f"Template resource: {param}"- templates = mcp.list_resource_templates()- assert len(templates) == 1- assert templates[0].tags == {"template", "test-tag"}+ templates_dict = await mcp.get_resource_templates()+ template = templates_dict["resource://{param}"]+ assert template.tags == {"template", "test-tag"}class TestPromptDecorator:@@ -504,18 +537,17 @@ class TestPromptDecorator:mcp = FastMCP()@mcp.prompt()- def test_prompt() -> str:+ def fn() -> str:return "Hello, world!"- prompts = mcp.list_prompts()- assert len(prompts) == 1- assert prompts[0].name == "test_prompt"-- result = await mcp.get_prompt("test_prompt")- assert len(result) == 1- message = result[0]- assert isinstance(message.content, TextContent)- assert message.content.text == "Hello, world!"+ prompts_dict = await mcp.get_prompts()+ assert len(prompts_dict) == 1+ prompt = prompts_dict["fn"]+ assert prompt.name == "fn"+ # Don't compare functions directly since validate_call wraps them+ content = await prompt.render()+ assert isinstance(content[0].content, TextContent)+ assert content[0].content.text == "Hello, world!"async def test_prompt_decorator_incorrect_usage(self):mcp = FastMCP()@@ -525,36 +557,38 @@ class TestPromptDecorator:):@mcp.prompt # Missing parentheses #type: ignore- def test_prompt() -> str:+ def fn() -> str:return "Hello, world!"async def test_prompt_decorator_with_name(self):mcp = FastMCP()- @mcp.prompt(name="custom-prompt")- def test_prompt() -> str:+ @mcp.prompt(name="custom_name")+ def fn() -> str:return "Hello, world!"- prompts = mcp.list_prompts()- assert len(prompts) == 1- assert prompts[0].name == "custom-prompt"-- result = await mcp.get_prompt("custom-prompt")- assert len(result) == 1- message = result[0]- assert isinstance(message.content, TextContent)- assert message.content.text == "Hello, world!"+ prompts_dict = await mcp.get_prompts()+ assert len(prompts_dict) == 1+ prompt = prompts_dict["custom_name"]+ assert prompt.name == "custom_name"+ content = await prompt.render()+ assert isinstance(content[0].content, TextContent)+ assert content[0].content.text == "Hello, world!"async def test_prompt_decorator_with_description(self):mcp = FastMCP()- @mcp.prompt(description="Test prompt description")- def test_prompt() -> str:+ @mcp.prompt(description="A custom description")+ def fn() -> str:return "Hello, world!"- prompts = mcp.list_prompts()- assert len(prompts) == 1- assert prompts[0].description == "Test prompt description"+ prompts_dict = await mcp.get_prompts()+ assert len(prompts_dict) == 1+ prompt = prompts_dict["fn"]+ assert prompt.description == "A custom description"+ content = await prompt.render()+ assert isinstance(content[0].content, TextContent)+ assert content[0].content.text == "Hello, world!"async def test_prompt_decorator_with_parameters(self):mcp = FastMCP()@@ -563,28 +597,30 @@ class TestPromptDecorator:def test_prompt(name: str, greeting: str = "Hello") -> str:return f"{greeting}, {name}!"- prompts = mcp.list_prompts()- assert len(prompts) == 1- assert prompts[0].arguments is not None- assert len(prompts[0].arguments) == 2- assert prompts[0].arguments[0].name == "name"- assert prompts[0].arguments[0].required is True- assert prompts[0].arguments[1].name == "greeting"- assert prompts[0].arguments[1].required is False-- result = await mcp.get_prompt("test_prompt", {"name": "World"})- assert len(result) == 1- message = result[0]- assert isinstance(message.content, TextContent)- assert message.content.text == "Hello, World!"-- result = await mcp.get_prompt(- "test_prompt", {"name": "World", "greeting": "Hi"}- )- assert len(result) == 1- message = result[0]- assert isinstance(message.content, TextContent)- assert message.content.text == "Hi, World!"+ prompts_dict = await mcp.get_prompts()+ assert len(prompts_dict) == 1+ prompt = prompts_dict["test_prompt"]+ assert prompt.arguments is not None+ assert len(prompt.arguments) == 2+ assert prompt.arguments[0].name == "name"+ assert prompt.arguments[0].required is True+ assert prompt.arguments[1].name == "greeting"+ assert prompt.arguments[1].required is False++ async with Client(mcp) as client:+ result = await client.get_prompt("test_prompt", {"name": "World"})+ assert len(result) == 1+ message = result[0]+ assert isinstance(message.content, TextContent)+ assert message.content.text == "Hello, World!"++ result = await client.get_prompt(+ "test_prompt", {"name": "World", "greeting": "Hi"}+ )+ assert len(result) == 1+ message = result[0]+ assert isinstance(message.content, TextContent)+ assert message.content.text == "Hi, World!"async def test_prompt_decorator_instance_method(self):mcp = FastMCP()@@ -599,11 +635,12 @@ class TestPromptDecorator:obj = MyClass("My prefix:")mcp.add_prompt(obj.test_prompt, name="test_prompt")- result = await mcp.get_prompt("test_prompt")- assert len(result) == 1- message = result[0]- assert isinstance(message.content, TextContent)- assert message.content.text == "My prefix: Hello, world!"+ async with Client(mcp) as client:+ result = await client.get_prompt("test_prompt")+ assert len(result) == 1+ message = result[0]+ assert isinstance(message.content, TextContent)+ assert message.content.text == "My prefix: Hello, world!"async def test_prompt_decorator_classmethod(self):mcp = FastMCP()@@ -617,11 +654,12 @@ class TestPromptDecorator:mcp.add_prompt(MyClass.test_prompt, name="test_prompt")- result = await mcp.get_prompt("test_prompt")- assert len(result) == 1- message = result[0]- assert isinstance(message.content, TextContent)- assert message.content.text == "Class prefix: Hello, world!"+ async with Client(mcp) as client:+ result = await client.get_prompt("test_prompt")+ assert len(result) == 1+ message = result[0]+ assert isinstance(message.content, TextContent)+ assert message.content.text == "Class prefix: Hello, world!"async def test_prompt_decorator_staticmethod(self):mcp = FastMCP()@@ -632,11 +670,12 @@ class TestPromptDecorator:def test_prompt() -> str:return "Static Hello, world!"- result = await mcp.get_prompt("test_prompt")- assert len(result) == 1- message = result[0]- assert isinstance(message.content, TextContent)- assert message.content.text == "Static Hello, world!"+ async with Client(mcp) as client:+ result = await client.get_prompt("test_prompt")+ assert len(result) == 1+ message = result[0]+ assert isinstance(message.content, TextContent)+ assert message.content.text == "Static Hello, world!"async def test_prompt_decorator_async_function(self):mcp = FastMCP()@@ -645,22 +684,25 @@ class TestPromptDecorator:async def test_prompt() -> str:return "Async Hello, world!"- result = await mcp.get_prompt("test_prompt")- assert len(result) == 1- message = result[0]- assert isinstance(message.content, TextContent)- assert message.content.text == "Async Hello, world!"+ async with Client(mcp) as client:+ result = await client.get_prompt("test_prompt")+ assert len(result) == 1+ message = result[0]+ assert isinstance(message.content, TextContent)+ assert message.content.text == "Async Hello, world!"async def test_prompt_decorator_with_tags(self):+ """Test that the prompt decorator properly sets tags."""mcp = FastMCP()@mcp.prompt(tags={"example", "test-tag"})- def test_prompt() -> str:+ def sample_prompt() -> str:return "Hello, world!"- prompts = mcp.list_prompts()- assert len(prompts) == 1- assert prompts[0].tags == {"example", "test-tag"}+ prompts_dict = await mcp.get_prompts()+ assert len(prompts_dict) == 1+ prompt = prompts_dict["sample_prompt"]+ assert prompt.tags == {"example", "test-tag"}@pytest.fixture@@ -710,7 +752,7 @@ class TestServerTools:assert len(await tool_server._mcp_list_tools()) == 6async def test_call_tool(self, tool_server: FastMCP):- result = await tool_server.call_tool("add", {"x": 1, "y": 2})+ result = await tool_server._mcp_call_tool("add", {"x": 1, "y": 2})assert isinstance(result[0], TextContent)assert result[0].text == "3"@@ -722,7 +764,7 @@ class TestServerTools:async def test_call_tool_error(self, tool_server: FastMCP):with pytest.raises(ToolError):- await tool_server.call_tool("error_tool", {})+ await tool_server._mcp_call_tool("error_tool", {})async def test_call_tool_error_as_client(self, tool_server: FastMCP):async with Client(tool_server) as client:@@ -737,7 +779,7 @@ class TestServerTools:assert "Test error" in result.content[0].textasync def test_tool_returns_list(self, tool_server: FastMCP):- result = await tool_server.call_tool("list_tool", {})+ result = await tool_server._mcp_call_tool("list_tool", {})assert isinstance(result[0], TextContent)assert result[0].text == '["x", 2]'@@ -746,7 +788,9 @@ class TestServerTools:image_path = tmp_path / "test.png"image_path.write_bytes(b"fake png data")- result = await tool_server.call_tool("image_tool", {"path": str(image_path)})+ result = await tool_server._mcp_call_tool(+ "image_tool", {"path": str(image_path)}+ )content = result[0]assert isinstance(content, ImageContent)assert content.type == "image"@@ -756,7 +800,7 @@ class TestServerTools:assert decoded == b"fake png data"async def test_tool_mixed_content(self, tool_server: FastMCP):- result = await tool_server.call_tool("mixed_content_tool", {})+ result = await tool_server._mcp_call_tool("mixed_content_tool", {})assert len(result) == 2content1 = result[0]content2 = result[1]@@ -775,7 +819,7 @@ class TestServerTools:image_path = tmp_path / "test.png"image_path.write_bytes(b"test image data")- result = await tool_server.call_tool(+ result = await tool_server._mcp_call_tool("mixed_list_fn", {"image_path": str(image_path)})assert len(result) == 3@@ -993,8 +1037,7 @@ class TestServerResourceTemplates:assert result[0].text == "Static data"async def test_template_with_default_params(self):- """Test that a template with default function parameters works when those parameters- are not in the URI template"""+ """Test that a template can have default parameters."""mcp = FastMCP()@mcp.resource("math://add/{x}")@@ -1002,7 +1045,8 @@ class TestServerResourceTemplates:return x + y# Verify it's registered as a template- templates = mcp.list_resource_templates()+ templates_dict = await mcp.get_resource_templates()+ templates = list(templates_dict.values())assert len(templates) == 1assert templates[0].uri_template == "math://add/{x}"@@ -1019,16 +1063,18 @@ class TestServerResourceTemplates:assert result == "17" # 7 + default 10async def test_template_to_resource_conversion(self):- """Test that templates are properly converted to resources when accessed"""+ """Test that a template can be converted to a resource."""mcp = FastMCP()@mcp.resource("resource://{name}/data")def get_data(name: str) -> str:return f"Data for {name}"- # Should be registered as a template- assert len(mcp._resource_manager._templates) == 1- assert len(await mcp._mcp_list_resources()) == 0+ # Verify it's registered as a template+ templates_dict = await mcp.get_resource_templates()+ templates = list(templates_dict.values())+ assert len(templates) == 1+ assert templates[0].uri_template == "resource://{name}/data"# When accessed, should create a concrete resourceresource = await mcp._resource_manager.get_resource("resource://test/data")@@ -1037,30 +1083,32 @@ class TestServerResourceTemplates:assert result == "Data for test"async def test_stacked_resource_template_decorators(self):- """Test that multiple resource decorators can be stacked on the same function."""+ """Test that resource template decorators can be stacked."""mcp = FastMCP()- # Define a function with multiple stacked resource decorators@mcp.resource("users://email/{email}")@mcp.resource("users://name/{name}")def lookup_user(name: str | None = None, email: str | None = None) -> dict:- """Look up a user by either name or email."""- # In a real implementation, this would query a database- if email:+ if name:return {- "found_by": "email",- "name": f"User for {email}",- "email": email,+ "id": "123",+ "name": name,+ "email": "[email protected]",+ "lookup": "name",}- else:+ elif email:return {- "found_by": "name",- "name": name,- "email": f"{name.lower()}@example.com" if name else None,+ "id": "123",+ "name": "Test User",+ "email": email,+ "lookup": "email",}+ else:+ raise ValueError("Either name or email must be provided")# Verify both templates are registered- templates = mcp.list_resource_templates()+ templates_dict = await mcp.get_resource_templates()+ templates = list(templates_dict.values())assert len(templates) == 2template_uris = {t.uri_template for t in templates}assert "users://email/{email}" in template_uris@@ -1073,16 +1121,27 @@ class TestServerResourceTemplates:)assert isinstance(email_result[0], TextResourceContents)email_data = json.loads(email_result[0].text)- assert email_data["found_by"] == "email"+ assert email_data["lookup"] == "email"assert email_data["email"] == "[email protected]"# Test lookup by namename_result = await client.read_resource(AnyUrl("users://name/John"))assert isinstance(name_result[0], TextResourceContents)name_data = json.loads(name_result[0].text)- assert name_data["found_by"] == "name"+ assert name_data["lookup"] == "name"assert name_data["name"] == "John"- assert name_data["email"] == "[email protected]"+ assert name_data["email"] == "[email protected]"++ async def test_template_decorator_with_tags(self):+ mcp = FastMCP()++ @mcp.resource("resource://{param}", tags={"template", "test-tag"})+ def template_resource(param: str) -> str:+ return f"Template resource: {param}"++ templates_dict = await mcp.get_resource_templates()+ template = templates_dict["resource://{param}"]+ assert template.tags == {"template", "test-tag"}class TestContextInjection:@@ -1219,11 +1278,12 @@ class TestServerPrompts:def fn() -> str:return "Hello, world!"- prompts = mcp._prompt_manager.list_prompts()- assert len(prompts) == 1- assert prompts[0].name == "fn"+ prompts_dict = await mcp.get_prompts()+ assert len(prompts_dict) == 1+ prompt = prompts_dict["fn"]+ assert prompt.name == "fn"# Don't compare functions directly since validate_call wraps them- content = await prompts[0].render()+ content = await prompt.render()assert isinstance(content[0].content, TextContent)assert content[0].content.text == "Hello, world!"@@ -1235,10 +1295,11 @@ class TestServerPrompts:def fn() -> str:return "Hello, world!"- prompts = mcp._prompt_manager.list_prompts()- assert len(prompts) == 1- assert prompts[0].name == "custom_name"- content = await prompts[0].render()+ prompts_dict = await mcp.get_prompts()+ assert len(prompts_dict) == 1+ prompt = prompts_dict["custom_name"]+ assert prompt.name == "custom_name"+ content = await prompt.render()assert isinstance(content[0].content, TextContent)assert content[0].content.text == "Hello, world!"@@ -1250,10 +1311,11 @@ class TestServerPrompts:def fn() -> str:return "Hello, world!"- prompts = mcp._prompt_manager.list_prompts()- assert len(prompts) == 1- assert prompts[0].description == "A custom description"- content = await prompts[0].render()+ prompts_dict = await mcp.get_prompts()+ assert len(prompts_dict) == 1+ prompt = prompts_dict["fn"]+ assert prompt.description == "A custom description"+ content = await prompt.render()assert isinstance(content[0].content, TextContent)assert content[0].content.text == "Hello, world!"@@ -1272,20 +1334,22 @@ class TestServerPrompts:@mcp.prompt()def fn(name: str, optional: str = "default") -> str:- return f"Hello, {name}!"+ return f"Hello, {name}! {optional}"++ prompts_dict = await mcp.get_prompts()+ assert len(prompts_dict) == 1async with Client(mcp) as client:- result = await client.list_prompts()- assert result is not None- assert len(result) == 1- prompt = result[0]- assert prompt.name == "fn"- assert prompt.arguments is not None- assert len(prompt.arguments) == 2- assert prompt.arguments[0].name == "name"- assert prompt.arguments[0].required is True- assert prompt.arguments[1].name == "optional"- assert prompt.arguments[1].required is False+ prompts = await client.list_prompts()+ assert len(prompts) == 1+ assert prompts[0].name == "fn"+ assert prompts[0].description is None+ assert prompts[0].arguments is not None+ assert len(prompts[0].arguments) == 2+ assert prompts[0].arguments[0].name == "name"+ assert prompts[0].arguments[0].required is True+ assert prompts[0].arguments[1].name == "optional"+ assert prompts[0].arguments[1].required is Falseasync def test_get_prompt(self):"""Test getting a prompt through MCP protocol."""@@ -1297,8 +1361,8 @@ class TestServerPrompts:async with Client(mcp) as client:result = await client.get_prompt("fn", {"name": "World"})- assert len(result.messages) == 1- message = result.messages[0]+ assert len(result) == 1+ message = result[0]assert message.role == "user"content = message.contentassert isinstance(content, TextContent)@@ -1323,8 +1387,8 @@ class TestServerPrompts:async with Client(mcp) as client:result = await client.get_prompt("fn")- assert result.messages[0].role == "user"- content = result.messages[0].content+ assert result[0].role == "user"+ content = result[0].contentassert isinstance(content, EmbeddedResource)resource = content.resourceassert isinstance(resource, TextResourceContents)@@ -1334,8 +1398,8 @@ class TestServerPrompts:async def test_get_unknown_prompt(self):"""Test error when getting unknown prompt."""mcp = FastMCP()- async with Client(mcp) as client:- with pytest.raises(McpError, match="Unknown prompt"):+ with pytest.raises(ClientError, match="Unknown prompt"):+ async with Client(mcp) as client:await client.get_prompt("unknown")async def test_get_prompt_missing_args(self):@@ -1346,8 +1410,8 @@ class TestServerPrompts:def prompt_fn(name: str) -> str:return f"Hello, {name}!"- async with Client(mcp) as client:- with pytest.raises(McpError, match="Missing required arguments"):+ with pytest.raises(ClientError, match="Missing required arguments"):+ async with Client(mcp) as client:await client.get_prompt("prompt_fn")async def test_tool_decorator_with_tags(self):@@ -1364,15 +1428,15 @@ class TestServerPrompts:assert tools[0].tags == {"example", "test-tag"}async def test_resource_decorator_with_tags(self):- """Test that the resource decorator properly sets tags."""+ """Test that the resource decorator supports tags."""mcp = FastMCP()- @mcp.resource("resource://sample", tags={"example", "test-tag"})- def sample_resource() -> str:- return "Sample resource"+ @mcp.resource("resource://data", tags={"example", "test-tag"})+ def get_data() -> str:+ return "Hello, world!"- # Verify the tags were set correctly- resources = mcp._resource_manager.list_resources()+ resources_dict = await mcp.get_resources()+ resources = list(resources_dict.values())assert len(resources) == 1assert resources[0].tags == {"example", "test-tag"}@@ -1384,9 +1448,9 @@ class TestServerPrompts:def template_resource(param: str) -> str:return f"Template resource: {param}"- templates = mcp._resource_manager.list_templates()- assert len(templates) == 1- assert templates[0].tags == {"template", "test-tag"}+ templates_dict = await mcp.get_resource_templates()+ template = templates_dict["resource://{param}"]+ assert template.tags == {"template", "test-tag"}async def test_prompt_decorator_with_tags(self):"""Test that the prompt decorator properly sets tags."""@@ -1394,9 +1458,9 @@ class TestServerPrompts:@mcp.prompt(tags={"example", "test-tag"})def sample_prompt() -> str:- return "Sample prompt"+ return "Hello, world!"- # Verify the tags were set correctly- prompts = mcp._prompt_manager.list_prompts()- assert len(prompts) == 1- assert prompts[0].tags == {"example", "test-tag"}+ prompts_dict = await mcp.get_prompts()+ assert len(prompts_dict) == 1+ prompt = prompts_dict["sample_prompt"]+ assert prompt.tags == {"example", "test-tag"}diff --git a/tests/tools/test_tool_manager.py b/tests/tools/test_tool_manager.pyindex 2c3639b56..de86b5192 100644--- a/tests/tools/test_tool_manager.py+++ b/tests/tools/test_tool_manager.py@@ -4,7 +4,7 @@ import loggingimport pytestfrom pydantic import BaseModel-from fastmcp.exceptions import ToolError+from fastmcp.exceptions import NotFoundError, ToolErrorfrom fastmcp.tools import ToolManagerfrom fastmcp.tools.tool import Tool@@ -235,23 +235,6 @@ class TestToolTags:assert len(utility_tools) == 2assert {tool.name for tool in utility_tools} == {"string_tool", "mixed_tool"}- def test_import_tools_preserves_tags(self):- """Test that importing tools preserves their tags."""-- def tagged_tool(x: int) -> int:- """A tool with tags."""- return x-- source_manager = ToolManager()- source_manager.add_tool_from_fn(tagged_tool, tags={"test", "example"})-- target_manager = ToolManager()- target_manager.import_tools(source_manager, "source/")-- imported_tool = target_manager.get_tool("source/tagged_tool")- assert imported_tool is not None- assert imported_tool.tags == {"test", "example"}-class TestCallTools:async def test_call_tool(self):@@ -262,7 +245,13 @@ class TestCallTools:manager = ToolManager()manager.add_tool_from_fn(add)result = await manager.call_tool("add", {"a": 1, "b": 2})- assert result == 3+ assert isinstance(result, list)+ assert len(result) == 1+ from mcp.types import TextContent++ assert isinstance(result[0], TextContent)+ assert result[0].text == "3"+ assert json.loads(result[0].text) == 3async def test_call_async_tool(self):async def double(n: int) -> int:@@ -272,7 +261,13 @@ class TestCallTools:manager = ToolManager()manager.add_tool_from_fn(double)result = await manager.call_tool("double", {"n": 5})- assert result == 10+ assert isinstance(result, list)+ assert len(result) == 1+ from mcp.types import TextContent++ assert isinstance(result[0], TextContent)+ assert result[0].text == "10"+ assert json.loads(result[0].text) == 10async def test_call_tool_with_default_args(self):def add(a: int, b: int = 1) -> int:@@ -282,7 +277,13 @@ class TestCallTools:manager = ToolManager()manager.add_tool_from_fn(add)result = await manager.call_tool("add", {"a": 1})- assert result == 2+ assert isinstance(result, list)+ assert len(result) == 1+ from mcp.types import TextContent++ assert isinstance(result[0], TextContent)+ assert result[0].text == "2"+ assert json.loads(result[0].text) == 2async def test_call_tool_with_missing_args(self):def add(a: int, b: int) -> int:@@ -296,7 +297,7 @@ class TestCallTools:async def test_call_unknown_tool(self):manager = ToolManager()- with pytest.raises(ToolError):+ with pytest.raises(NotFoundError, match="Unknown tool: unknown"):await manager.call_tool("unknown", {"a": 1})async def test_call_tool_with_list_int_input(self):@@ -306,10 +307,21 @@ class TestCallTools:manager = ToolManager()manager.add_tool_from_fn(sum_vals)# Try both with plain list and with JSON list+ from mcp.types import TextContent+result = await manager.call_tool("sum_vals", {"vals": "[1, 2, 3]"})- assert result == 6+ assert isinstance(result, list)+ assert len(result) == 1+ assert isinstance(result[0], TextContent)+ assert result[0].text == "6"+ assert json.loads(result[0].text) == 6+result = await manager.call_tool("sum_vals", {"vals": [1, 2, 3]})- assert result == 6+ assert isinstance(result, list)+ assert len(result) == 1+ assert isinstance(result[0], TextContent)+ assert result[0].text == "6"+ assert json.loads(result[0].text) == 6async def test_call_tool_with_list_str_or_str_input(self):def concat_strs(vals: list[str] | str) -> str:@@ -317,17 +329,36 @@ class TestCallTools:manager = ToolManager()manager.add_tool_from_fn(concat_strs)+ from mcp.types import TextContent+# Try both with plain python object and with JSON listresult = await manager.call_tool("concat_strs", {"vals": ["a", "b", "c"]})- assert result == "abc"+ assert isinstance(result, list)+ assert len(result) == 1+ assert isinstance(result[0], TextContent)+ assert result[0].text == "abc"+result = await manager.call_tool("concat_strs", {"vals": '["a", "b", "c"]'})- assert result == "abc"+ assert isinstance(result, list)+ assert len(result) == 1+ assert isinstance(result[0], TextContent)+ assert result[0].text == "abc"+result = await manager.call_tool("concat_strs", {"vals": "a"})- assert result == "a"+ assert isinstance(result, list)+ assert len(result) == 1+ assert isinstance(result[0], TextContent)+ assert result[0].text == "a"+result = await manager.call_tool("concat_strs", {"vals": '"a"'})- assert result == '"a"'+ assert isinstance(result, list)+ assert len(result) == 1+ assert isinstance(result[0], TextContent)+ assert result[0].text == '"a"'async def test_call_tool_with_complex_model(self):+ from mcp.types import TextContent+from fastmcp import Contextclass MyShrimpTank(BaseModel):@@ -342,16 +373,26 @@ class TestCallTools:manager = ToolManager()manager.add_tool_from_fn(name_shrimp)+result = await manager.call_tool("name_shrimp",{"tank": {"x": None, "shrimp": [{"name": "rex"}, {"name": "gertrude"}]}},)- assert result == ["rex", "gertrude"]+ assert isinstance(result, list)+ assert len(result) == 1+ assert isinstance(result[0], TextContent)+ assert result[0].text == '["rex", "gertrude"]'+ assert json.loads(result[0].text) == ["rex", "gertrude"]+result = await manager.call_tool("name_shrimp",{"tank": '{"x": null, "shrimp": [{"name": "rex"}, {"name": "gertrude"}]}'},)- assert result == ["rex", "gertrude"]+ assert isinstance(result, list)+ assert len(result) == 1+ assert isinstance(result[0], TextContent)+ assert result[0].text == '["rex", "gertrude"]'+ assert json.loads(result[0].text) == ["rex", "gertrude"]class TestToolSchema:@@ -391,6 +432,8 @@ class TestContextHandling:async def test_context_injection(self):"""Test that context is properly injected during tool execution."""+ from mcp.types import TextContent+from fastmcp import Context, FastMCPdef tool_with_context(x: int, ctx: Context) -> str:@@ -403,10 +446,15 @@ class TestContextHandling:mcp = FastMCP()ctx = mcp.get_context()result = await manager.call_tool("tool_with_context", {"x": 42}, context=ctx)- assert result == "42"+ assert isinstance(result, list)+ assert len(result) == 1+ assert isinstance(result[0], TextContent)+ assert result[0].text == "42"async def test_context_injection_async(self):"""Test that context is properly injected in async tools."""+ from mcp.types import TextContent+from fastmcp import Context, FastMCPasync def async_tool(x: int, ctx: Context) -> str:@@ -419,10 +467,15 @@ class TestContextHandling:mcp = FastMCP()ctx = mcp.get_context()result = await manager.call_tool("async_tool", {"x": 42}, context=ctx)- assert result == "42"+ assert isinstance(result, list)+ assert len(result) == 1+ assert isinstance(result[0], TextContent)+ assert result[0].text == "42"async def test_context_optional(self):"""Test that context is optional when calling tools."""+ from mcp.types import TextContent+from fastmcp import Contextdef tool_with_context(x: int, ctx: Context | None = None) -> str:@@ -432,7 +485,10 @@ class TestContextHandling:manager.add_tool_from_fn(tool_with_context)# Should not raise an error when context is not providedresult = await manager.call_tool("tool_with_context", {"x": 42})- assert result == "42"+ assert isinstance(result, list)+ assert len(result) == 1+ assert isinstance(result[0], TextContent)+ assert result[0].text == "42"async def test_context_error_handling(self):"""Test error handling when context injection fails."""@@ -450,112 +506,6 @@ class TestContextHandling:await manager.call_tool("tool_with_context", {"x": 42}, context=ctx)-class TestImportTools:- def test_import_tools(self):- """Test importing tools from one manager to another with a prefix."""- # Setup source manager with tools- source_manager = ToolManager()-- # Create some test tools- def tool1_fn():- return "Tool 1 result"-- def tool2_fn():- return "Tool 2 result"-- # Add tools to source manager- source_manager.add_tool_from_fn(- tool1_fn, name="get_data", description="Get some data"- )- source_manager.add_tool_from_fn(- tool2_fn, name="process_data", description="Process the data"- )-- # Create target manager- target_manager = ToolManager()-- # Import tools from source to target- prefix = "source/"- target_manager.import_tools(source_manager, prefix)-- # Verify tools were imported with prefixes- assert "source/get_data" in target_manager._tools- assert "source/process_data" in target_manager._tools-- # Verify the original tools still exist in source manager- assert "get_data" in source_manager._tools- assert "process_data" in source_manager._tools-- # Verify the imported tools have the correct descriptions- assert target_manager._tools["source/get_data"].description == "Get some data"- assert (- target_manager._tools["source/process_data"].description- == "Process the data"- )-- # Verify the tool functions were properly copied- # We can't directly compare functions, so we'll check their __name__ attribute- assert target_manager._tools["source/get_data"].fn == tool1_fn- assert target_manager._tools["source/process_data"].fn == tool2_fn-- def test_tool_duplicate_behavior(self):- """Test the behavior when importing tools with duplicate names."""- # Setup source and target managers- source_manager = ToolManager()- target_manager = ToolManager()-- # Add the same tool name to both managers- def source_fn():- return "Source result"-- def target_fn():- return "Target result"-- source_manager.add_tool_from_fn(source_fn, name="common_tool")- target_manager.add_tool_from_fn(- target_fn, name="source/common_tool"- ) # Pre-create with the prefixed name-- # Import tools from source to target- target_manager.import_tools(source_manager, "source/")-- # The original tool in the target manager is replaced by the imported one- assert target_manager._tools["source/common_tool"].fn == source_fn-- def test_import_tools_with_multiple_prefixes(self):- """Test importing tools from multiple managers with different prefixes."""- # Setup source managers- weather_manager = ToolManager()- news_manager = ToolManager()-- # Add tools to source managers- def forecast_fn():- return "Weather forecast"-- def headlines_fn():- return "News headlines"-- weather_manager.add_tool_from_fn(forecast_fn, name="forecast")- news_manager.add_tool_from_fn(headlines_fn, name="headlines")-- # Create target manager and import from both sources- main_manager = ToolManager()- main_manager.import_tools(weather_manager, "weather/")- main_manager.import_tools(news_manager, "news/")-- # Verify tools were imported with correct prefixes- assert "weather/forecast" in main_manager._tools- assert "news/headlines" in main_manager._tools-- # Verify the tools are accessible and functioning- assert (- main_manager._tools["weather/forecast"].fn.__name__ == forecast_fn.__name__- )- assert (- main_manager._tools["news/headlines"].fn.__name__ == headlines_fn.__name__- )--class TestCustomToolNames:"""Test adding tools with custom names that differ from their function names."""@@ -596,6 +546,7 @@ class TestCustomToolNames:async def test_call_tool_with_custom_name(self):"""Test calling a tool added with a custom name."""+ from mcp.types import TextContentdef multiply(a: int, b: int) -> int:"""Multiply two numbers."""@@ -606,30 +557,16 @@ class TestCustomToolNames:# Tool should be callable by its custom nameresult = await manager.call_tool("custom_multiply", {"a": 5, "b": 3})- assert result == 15+ assert isinstance(result, list)+ assert len(result) == 1+ assert isinstance(result[0], TextContent)+ assert result[0].text == "15"+ assert json.loads(result[0].text) == 15# Original name should not be registered- with pytest.raises(ToolError):+ with pytest.raises(NotFoundError, match="Unknown tool: multiply"):await manager.call_tool("multiply", {"a": 5, "b": 3})- def test_import_tools_with_custom_names(self):- """Test importing tools with custom names."""-- def source_fn(x: int) -> int:- return x * 2-- # Create a source manager with a tool using custom name- source_manager = ToolManager()- source_manager.add_tool_from_fn(source_fn, name="custom_source")-- # Import the tools to a target manager with a prefix- target_manager = ToolManager()- target_manager.import_tools(source_manager, "prefix/")-- # The tool should be imported with the prefixed custom name- assert target_manager.get_tool("prefix/custom_source") is not None- assert target_manager.get_tool("prefix/source_fn") is None-def test_replace_tool_keeps_original_name(self):"""Test that replacing a tool with "replace" keeps the original name."""diff --git a/tests/utilities/test_func_metadata.py b/tests/utilities/test_func_metadata.pyindex ee8037c7f..96d8c6f3b 100644--- a/tests/utilities/test_func_metadata.py+++ b/tests/utilities/test_func_metadata.py@@ -85,7 +85,6 @@ def complex_arguments_fn(return "ok!"async def test_complex_function_runtime_arg_validation_non_json():"""Test that basic non-JSON arguments are validated correctly"""meta = func_metadata(complex_arguments_fn)@@ -122,7 +121,6 @@ async def test_complex_function_runtime_arg_validation_non_json():)async def test_complex_function_runtime_arg_validation_with_json():"""Test that JSON string arguments are parsed and validated correctly"""meta = func_metadata(complex_arguments_fn)@@ -199,7 +197,6 @@ def test_skip_names():assert model.also_keep == 2.5 # type: ignoreasync def test_lambda_function():"""Test lambda function schema and validation"""fn = lambda x, y=5: x # noqa: E731diff --git a/uv.lock b/uv.lockindex 7f89175dd..6fb683270 100644--- a/uv.lock+++ b/uv.lock@@ -254,10 +254,11 @@ wheels = [[[package]]name = "fastmcp"-version = "2.1.2.dev9+55f3666"+version = "2.1.3.dev31+8d922d9"source = { editable = "." }dependencies = [{ name = "dotenv" },+ { name = "exceptiongroup" },{ name = "fastapi" },{ name = "mcp" },{ name = "openapi-pydantic" },@@ -284,6 +285,7 @@ dev = [[package.metadata]requires-dist = [{ name = "dotenv", specifier = ">=0.9.9" },+ { name = "exceptiongroup", specifier = ">=1.2.2" },{ name = "fastapi", specifier = ">=0.115.12" },{ name = "mcp", specifier = ">=1.6.0,<2.0.0" },{ name = "openapi-pydantic", specifier = ">=0.5.1" },