Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add instructions field to InitializeResult #150

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/mcp/server/fastmcp/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand All @@ -110,6 +112,10 @@ def __init__(self, name: str | None = None, **settings: Any):
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.

Expand Down
6 changes: 5 additions & 1 deletion src/mcp/server/lowlevel/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,12 @@ def __init__(


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]]
] = {
Expand Down Expand Up @@ -139,6 +142,7 @@ def pkg_version(package: str) -> str:
notification_options or NotificationOptions(),
experimental_capabilities or {},
),
instructions=self.instructions,
)

def get_capabilities(
Expand Down
1 change: 1 addition & 0 deletions src/mcp/server/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ class InitializationOptions(BaseModel):
server_name: str
server_version: str
capabilities: ServerCapabilities
instructions: str | None = None
1 change: 1 addition & 0 deletions src/mcp/server/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ async def _received_request(
name=self._init_options.server_name,
version=self._init_options.server_version,
),
instructions=self._init_options.instructions,
)
)
)
Expand Down
2 changes: 2 additions & 0 deletions src/mcp/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 2 additions & 0 deletions tests/client/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ async def mock_server():
prompts=None,
),
serverInfo=Implementation(name="mock-server", version="0.1.0"),
instructions="The server instructions.",
)
)

Expand Down Expand Up @@ -92,6 +93,7 @@ async def listen_session():
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
Expand Down
3 changes: 2 additions & 1 deletion tests/server/fastmcp/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@
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):
Expand Down
Loading