Skip to content

Commit

Permalink
Add instructions field to ServerSession and FastMCP
Browse files Browse the repository at this point in the history
  • Loading branch information
salman1993 committed Jan 13, 2025
1 parent 1351914 commit bc6746e
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 5 deletions.
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: 1 addition & 1 deletion tests/client/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ async def mock_server():
prompts=None,
),
serverInfo=Implementation(name="mock-server", version="0.1.0"),
instructions="The server instructions."
instructions="The server instructions.",
)
)

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

0 comments on commit bc6746e

Please sign in to comment.