Merge pull request #150 from salman1993/sm/add-instructions

feat: add instructions field to InitializeResult
This commit is contained in:
Justin Spahr-Summers 2025-01-13 18:06:53 +00:00 • committed by GitHub
commit 62a0af6f71
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 21 additions and 4 deletions

View file

@ -86,9 +86,11 @@ class Settings(BaseSettings):
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._mcp_server = MCPServer(name=name or "FastMCP")
self._mcp_server = MCPServer(name=name or "FastMCP", instructions=instructions)
self._tool_manager = ToolManager(
warn_on_duplicate_tools=self.settings.warn_on_duplicate_tools
)
@ -110,6 +112,10 @@ class FastMCP:
def name(self) -> str:
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:
"""Run the FastMCP server. Note this is a synchronous function.

View file

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

View file

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

View file

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

View file

@ -285,6 +285,8 @@ class InitializeResult(Result):
"""The version of the Model Context Protocol that the server wants to use."""
capabilities: ServerCapabilities
serverInfo: Implementation
instructions: str | None = None
"""Instructions describing how to use the server and its features."""
class InitializedNotification(Notification):

View file

@ -51,6 +51,7 @@ async def test_client_session_initialize():
prompts=None,
),
serverInfo=Implementation(name="mock-server", version="0.1.0"),
instructions="The server instructions.",
)
)
@ -92,6 +93,7 @@ async def test_client_session_initialize():
assert result.protocolVersion == LATEST_PROTOCOL_VERSION
assert isinstance(result.capabilities, ServerCapabilities)
assert result.serverInfo == Implementation(name="mock-server", version="0.1.0")
assert result.instructions == "The server instructions."
# Check that the client sent the initialized notification
assert initialized_notification

View file

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