From 7231f12605e18ab75119e0a138c2e20376176ce4 Mon Sep 17 00:00:00 2001 From: Philipp Temminghoff Date: Fri, 29 Nov 2024 20:31:55 +0100 Subject: [PATCH] chore: improve resource listing --- src/llmling/server/server.py | 20 +++++++++++++++----- tests/server/test_mcp_protocol.py | 2 +- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/llmling/server/server.py b/src/llmling/server/server.py index 4e20a55..86730af 100644 --- a/src/llmling/server/server.py +++ b/src/llmling/server/server.py @@ -186,18 +186,28 @@ async def handle_list_resources() -> list[mcp.types.Resource]: resources = [] for name in self.runtime.list_resources(): try: - resource = await self.runtime.load_resource(name) + # First get URI and basic info without loading uri = self.runtime.get_resource_uri(name) + resource_config = self.runtime._config.resources[ + name + ] # Get raw config + mcp_resource = mcp.types.Resource( uri=conversions.to_mcp_uri(uri), - name=resource.metadata.name or name, - description=resource.metadata.description, - mimeType=resource.metadata.mime_type, + name=name, + description=resource_config.description, + mimeType="text/plain", # Default, could be made more specific ) resources.append(mcp_resource) + except Exception: - logger.exception("Failed to create resource %r", name) + logger.exception( + "Failed to create resource listing for %r. Config: %r", + name, + self.runtime._config.resources.get(name), + ) continue + return resources @self.server.read_resource() diff --git a/tests/server/test_mcp_protocol.py b/tests/server/test_mcp_protocol.py index 6c766ba..47afc28 100644 --- a/tests/server/test_mcp_protocol.py +++ b/tests/server/test_mcp_protocol.py @@ -72,7 +72,7 @@ async def test_mcp_resource_operations(configured_client: MCPInProcSession) -> N assert "resources" in response resource_list = response["resources"] assert len(resource_list) >= 1 - test_resource = next(r for r in resource_list if r["name"] == "Test resource") + test_resource = next(r for r in resource_list if r["description"] == "Test resource") # Read resource content response = await configured_client.send_request(