feat: route mcp runtime resource reads

This commit is contained in:
lda
2026-06-07 18:56:10 +07:00 Verified
parent 75e33f2fa8
commit 87fff2a110
7 changed files with 97 additions and 3 deletions
+8
View File
@@ -42,6 +42,7 @@ class PersistentSessionFactory:
connection=connection,
auth=auth,
call_callback=owner.call_tool,
read_resource_callback=owner.read_resource,
close_callback=owner.close,
)
@@ -143,6 +144,13 @@ class _SessionOwner:
run=lambda client: client.call_tool(tool_name, payload),
)
async def read_resource(self, uri: str) -> dict[str, Any]:
"""Submit a resource read through the generic owner-task operation queue."""
return await self.submit(
operation="read_resource",
run=lambda client: client.read_resource(uri),
)
async def close(self) -> None:
"""Ask the owner task to close the MCP transport in its own scope."""
task = self._task
+9
View File
@@ -83,6 +83,15 @@ class McpRuntimePool:
session = await self.get_session(connection, auth)
return await session.call_tool(tool_name, payload)
async def read_resource(
self,
connection: McpSourceConnection,
auth: AuthRecord | None,
uri: str,
) -> dict[str, Any]:
session = await self.get_session(connection, auth)
return await session.read_resource(uri)
async def close_connection(self, connection_id: str) -> None:
current = self._sessions.pop(connection_id, None)
if current is not None:
+12
View File
@@ -5,6 +5,7 @@ from dataclasses import dataclass
from typing import Any
from mcp.client.session import ClientSession
from pydantic import AnyUrl
from wf_sources_mcp.auth import AuthRecord
from wf_sources_mcp.connections import McpSourceConnection
@@ -12,6 +13,7 @@ from wf_sources_mcp.sdk import ToolCallResult
from wf_sources_mcp.sdk.converters import tool_result_to_call_result
RawToolCaller = Callable[[str, dict[str, Any]], Awaitable[ToolCallResult]]
RawResourceReader = Callable[[str], Awaitable[dict[str, Any]]]
@dataclass(slots=True)
@@ -32,6 +34,7 @@ class PersistentMcpSession:
auth: AuthRecord | None
client: ClientSession | None = None
call_callback: RawToolCaller | None = None
read_resource_callback: RawResourceReader | None = None
close_callback: Callable[[], Awaitable[None]] | None = None
async def call_tool(
@@ -44,6 +47,15 @@ class PersistentMcpSession:
return tool_result_to_call_result(result)
raise RuntimeError("persistent MCP session has no tool call transport")
async def read_resource(self, uri: str) -> dict[str, Any]:
"""Read an MCP resource through the owner task or injected session."""
if self.read_resource_callback is not None:
return await self.read_resource_callback(uri)
if self.client is not None:
result = await self.client.read_resource(AnyUrl(uri))
return result.model_dump(by_alias=True, mode="json", exclude_none=True)
raise RuntimeError("persistent MCP session has no resource read transport")
async def close(self) -> None:
"""Close the transport/session stack owned by the runtime factory."""
if self.close_callback is not None: