Skip to content

Commit b6380bb

Browse files
committed
show-server-info
1 parent 2ea1495 commit b6380bb

File tree

1 file changed

+51
-2
lines changed

1 file changed

+51
-2
lines changed

src/mcp/server/fastmcp/server.py

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ class Settings(BaseSettings, Generic[LifespanResultT]):
8585
# prompt settings
8686
warn_on_duplicate_prompts: bool = True
8787

88+
show_server_info: bool = True
89+
8890
dependencies: list[str] = Field(
8991
default_factory=list,
9092
description="List of dependencies to install in the server environment",
@@ -155,10 +157,57 @@ def run(self, transport: Literal["stdio", "sse"] = "stdio") -> None:
155157
if transport not in TRANSPORTS.__args__: # type: ignore
156158
raise ValueError(f"Unknown transport: {transport}")
157159

160+
async def run(): await self._run(transport)
161+
anyio.run(run)
162+
163+
async def _run(self, transport: Literal["stdio", "sse"]):
164+
if self.settings.show_server_info:
165+
server_info = await self.get_server_info()
166+
logger.debug(f"Server name: {server_info.name}")
167+
logger.debug(f"Server: {server_info.host}:{server_info.port}")
168+
for asset_type, asset_list in server_info.assets.items():
169+
if not asset_list:
170+
continue
171+
logger.debug(f"{asset_type}:")
172+
for asset in asset_list:
173+
logger.debug(f" - {asset.name} - {asset.description}")
174+
logger.debug("Server running...")
158175
if transport == "stdio":
159-
anyio.run(self.run_stdio_async)
176+
await self.run_stdio_async()
160177
else: # transport == "sse"
161-
anyio.run(self.run_sse_async)
178+
await self.run_sse_async()
179+
180+
async def get_server_info(self):
181+
"""
182+
Asynchronously retrieves and returns server information.
183+
184+
This method gathers details about the server, including its name, host, port,
185+
instructions, and various assets such as tools, resources, prompts, and resource templates.
186+
187+
Returns:
188+
dict: A dictionary containing the server's information:
189+
- name (str): The name of the server.
190+
- host (str): The host address of the server.
191+
- port (int): The port number the server is running on.
192+
- instructions (str): Instructions or guidelines related to the server.
193+
- assets (dict): A dictionary of server assets:
194+
- tools (list): A list of tools available on the server.
195+
- resources (list): A list of resources available on the server.
196+
- prompts (list): A list of prompts available on the server.
197+
- resource_templates (list): A list of resource templates available on the server.
198+
"""
199+
return {
200+
'name': self.name,
201+
'host': self.settings.host,
202+
'port': self.settings.port,
203+
'instructions': self.instructions,
204+
'assets': {
205+
'tools': await self.list_tools(),
206+
'resources': await self.list_resources(),
207+
'prompts': await self.list_prompts(),
208+
'resource_templates': await self.list_resource_templates()
209+
}
210+
}
162211

163212
def _setup_handlers(self) -> None:
164213
"""Set up core MCP protocol handlers."""

0 commit comments

Comments
 (0)