Add instructions field to ServerSession and FastMCP

This commit is contained in:
Salman Mohammed
2025-01-13 13:00:11 -05:00
parent 135191403a
commit bc6746e89b
6 changed files with 18 additions and 5 deletions

View File

@@ -86,9 +86,11 @@ class Settings(BaseSettings):
class FastMCP: class FastMCP:
def __init__(self, name: str | None = None, **settings: Any): def __init__(
self, name: str | None = None, instructions: str | None = None, **settings: Any
):
self.settings = Settings(**settings) self.settings = Settings(**settings)
self._mcp_server = MCPServer(name=name or "FastMCP") self._mcp_server = MCPServer(name=name or "FastMCP", instructions=instructions)
self._tool_manager = ToolManager( self._tool_manager = ToolManager(
warn_on_duplicate_tools=self.settings.warn_on_duplicate_tools warn_on_duplicate_tools=self.settings.warn_on_duplicate_tools
) )
@@ -110,6 +112,10 @@ class FastMCP:
def name(self) -> str: def name(self) -> str:
return self._mcp_server.name return self._mcp_server.name
@property
def instructions(self) -> str | None:
return self._mcp_server.instructions
def run(self, transport: Literal["stdio", "sse"] = "stdio") -> None: def run(self, transport: Literal["stdio", "sse"] = "stdio") -> None:
"""Run the FastMCP server. Note this is a synchronous function. """Run the FastMCP server. Note this is a synchronous function.

View File

@@ -101,9 +101,12 @@ class NotificationOptions:
class Server: class Server:
def __init__(self, name: str, version: str | None = None): def __init__(
self, name: str, version: str | None = None, instructions: str | None = None
):
self.name = name self.name = name
self.version = version self.version = version
self.instructions = instructions
self.request_handlers: dict[ self.request_handlers: dict[
type, Callable[..., Awaitable[types.ServerResult]] type, Callable[..., Awaitable[types.ServerResult]]
] = { ] = {
@@ -139,6 +142,7 @@ class Server:
notification_options or NotificationOptions(), notification_options or NotificationOptions(),
experimental_capabilities or {}, experimental_capabilities or {},
), ),
instructions=self.instructions,
) )
def get_capabilities( def get_capabilities(

View File

@@ -14,3 +14,4 @@ class InitializationOptions(BaseModel):
server_name: str server_name: str
server_version: str server_version: str
capabilities: ServerCapabilities capabilities: ServerCapabilities
instructions: str | None = None

View File

@@ -135,6 +135,7 @@ class ServerSession(
name=self._init_options.server_name, name=self._init_options.server_name,
version=self._init_options.server_version, version=self._init_options.server_version,
), ),
instructions=self._init_options.instructions,
) )
) )
) )

View File

@@ -51,7 +51,7 @@ async def test_client_session_initialize():
prompts=None, prompts=None,
), ),
serverInfo=Implementation(name="mock-server", version="0.1.0"), serverInfo=Implementation(name="mock-server", version="0.1.0"),
instructions="The server instructions." instructions="The server instructions.",
) )
) )

View File

@@ -27,8 +27,9 @@ if TYPE_CHECKING:
class TestServer: class TestServer:
@pytest.mark.anyio @pytest.mark.anyio
async def test_create_server(self): async def test_create_server(self):
mcp = FastMCP() mcp = FastMCP(instructions="Server instructions")
assert mcp.name == "FastMCP" assert mcp.name == "FastMCP"
assert mcp.instructions == "Server instructions"
@pytest.mark.anyio @pytest.mark.anyio
async def test_non_ascii_description(self): async def test_non_ascii_description(self):