feat: route mcp runtime prompt gets

This commit is contained in:
lda
2026-06-07 19:13:23 +07:00 Verified
parent f89801e7a8
commit b6bdae2020
7 changed files with 130 additions and 4 deletions
+12
View File
@@ -43,6 +43,7 @@ class PersistentSessionFactory:
auth=auth,
call_callback=owner.call_tool,
read_resource_callback=owner.read_resource,
get_prompt_callback=owner.get_prompt,
close_callback=owner.close,
)
@@ -151,6 +152,17 @@ class _SessionOwner:
run=lambda client: client.read_resource(uri),
)
async def get_prompt(
self,
prompt_name: str,
arguments: dict[str, str] | None = None,
) -> dict[str, Any]:
"""Submit a prompt get through the generic owner-task operation queue."""
return await self.submit(
operation="get_prompt",
run=lambda client: client.get_prompt(prompt_name, arguments),
)
async def close(self) -> None:
"""Ask the owner task to close the MCP transport in its own scope."""
task = self._task
+10
View File
@@ -92,6 +92,16 @@ class McpRuntimePool:
session = await self.get_session(connection, auth)
return await session.read_resource(uri)
async def get_prompt(
self,
connection: McpSourceConnection,
auth: AuthRecord | None,
prompt_name: str,
arguments: dict[str, str] | None = None,
) -> dict[str, Any]:
session = await self.get_session(connection, auth)
return await session.get_prompt(prompt_name, arguments)
async def close_connection(self, connection_id: str) -> None:
current = self._sessions.pop(connection_id, None)
if current is not None:
+18
View File
@@ -14,6 +14,10 @@ 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]]]
RawPromptGetter = Callable[
[str, dict[str, str] | None],
Awaitable[dict[str, Any]],
]
@dataclass(slots=True)
@@ -35,6 +39,7 @@ class PersistentMcpSession:
client: ClientSession | None = None
call_callback: RawToolCaller | None = None
read_resource_callback: RawResourceReader | None = None
get_prompt_callback: RawPromptGetter | None = None
close_callback: Callable[[], Awaitable[None]] | None = None
async def call_tool(
@@ -56,6 +61,19 @@ class PersistentMcpSession:
return result.model_dump(by_alias=True, mode="json", exclude_none=True)
raise RuntimeError("persistent MCP session has no resource read transport")
async def get_prompt(
self,
prompt_name: str,
arguments: dict[str, str] | None = None,
) -> dict[str, Any]:
"""Get an MCP prompt through the owner task or injected session."""
if self.get_prompt_callback is not None:
return await self.get_prompt_callback(prompt_name, arguments)
if self.client is not None:
result = await self.client.get_prompt(prompt_name, arguments)
return result.model_dump(by_alias=True, mode="json", exclude_none=True)
raise RuntimeError("persistent MCP session has no prompt transport")
async def close(self) -> None:
"""Close the transport/session stack owned by the runtime factory."""
if self.close_callback is not None: