refactor: move mcp sdk adapter to wf_sources_mcp
This commit is contained in:
@@ -7,6 +7,7 @@ from wf_api import file_workflow_stores
|
||||
from wf_config import WorkflowConfigFile
|
||||
from wf_config.models import FilesystemStoreConfig, McpSourceConfig, ServerConfig
|
||||
from wf_sources_mcp.runtime import McpRuntimePool, PersistentSessionFactory
|
||||
from wf_sources_mcp.sdk import McpSdkAdapter
|
||||
from wf_sources_mcp.source_registry import (
|
||||
FileSourceRegistryStore,
|
||||
workflow_mcp_source_to_connection_config,
|
||||
@@ -15,7 +16,6 @@ from wf_sources_mcp.storage import FileAuthStore, FileCatalogStore, FileStore
|
||||
|
||||
from ..control import BrokerConfigFile, ConnectionConfigFile
|
||||
from ..models import BrokerConfig
|
||||
from ..sdk import McpSdkAdapter
|
||||
from .models import BrokerStoreRoots
|
||||
from .service import WfMcpService
|
||||
|
||||
|
||||
@@ -12,10 +12,10 @@ from wf_api import (
|
||||
from wf_api.stores import WorkflowStores
|
||||
from wf_config import WorkflowConfigFile
|
||||
from wf_server import WorkflowServer, WorkflowServerConfig
|
||||
from wf_sources_mcp.sdk import McpSdkAdapter
|
||||
from wf_sources_mcp.source_registry import FileSourceRegistryStore, SourceRegistryStore
|
||||
|
||||
from ..models import BrokerConfig
|
||||
from ..sdk.adapter import McpSdkAdapter
|
||||
from .artifact_tools import register_artifact_tools
|
||||
from .config import broker_config_from_workflow_config, build_service_from_config
|
||||
from .prompts import register_broker_prompts
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
from wf_sources_mcp.sdk import BackendAdapter, ToolCallResult
|
||||
|
||||
from .adapter import McpSdkAdapter
|
||||
from wf_sources_mcp.sdk import BackendAdapter, McpSdkAdapter, ToolCallResult
|
||||
|
||||
__all__ = ["BackendAdapter", "McpSdkAdapter", "ToolCallResult"]
|
||||
|
||||
+3
-131
@@ -1,133 +1,5 @@
|
||||
from __future__ import annotations
|
||||
"""Compatibility shim for the canonical MCP SDK adapter."""
|
||||
|
||||
from contextlib import asynccontextmanager
|
||||
from typing import Any
|
||||
from wf_sources_mcp.sdk.adapter import McpSdkAdapter
|
||||
|
||||
from mcp import ClientResult
|
||||
from mcp.types import (
|
||||
ClientNotification,
|
||||
ClientRequest,
|
||||
ListPromptsResult,
|
||||
ListResourcesResult,
|
||||
ListToolsResult,
|
||||
)
|
||||
from pydantic import AnyUrl
|
||||
|
||||
from wf_sources_mcp.auth import AuthRecord
|
||||
from wf_sources_mcp.catalog import DiscoveredPrompt, DiscoveredResource, DiscoveredTool
|
||||
from wf_sources_mcp.client import open_mcp_session
|
||||
from wf_sources_mcp.connections import McpSourceConnection
|
||||
from wf_sources_mcp.sdk import BackendAdapter, ToolCallResult
|
||||
from wf_sources_mcp.sdk.converters import (
|
||||
prompt_to_discovered,
|
||||
resource_to_discovered,
|
||||
tool_result_to_call_result,
|
||||
tool_to_discovered,
|
||||
)
|
||||
|
||||
|
||||
class McpSdkAdapter(BackendAdapter):
|
||||
@asynccontextmanager
|
||||
async def _session(
|
||||
self,
|
||||
connection: McpSourceConnection,
|
||||
auth: AuthRecord | None,
|
||||
):
|
||||
async with open_mcp_session(connection, auth) as session:
|
||||
yield session
|
||||
|
||||
async def list_tools(
|
||||
self,
|
||||
connection: McpSourceConnection,
|
||||
auth: AuthRecord | None,
|
||||
) -> list[DiscoveredTool]:
|
||||
async with self._session(connection, auth) as session:
|
||||
result: ListToolsResult = await session.list_tools()
|
||||
return [tool_to_discovered(tool) for tool in result.tools]
|
||||
|
||||
async def list_resources(
|
||||
self,
|
||||
connection: McpSourceConnection,
|
||||
auth: AuthRecord | None,
|
||||
) -> list[DiscoveredResource]:
|
||||
async with self._session(connection, auth) as session:
|
||||
result: ListResourcesResult = await session.list_resources()
|
||||
return [resource_to_discovered(resource) for resource in result.resources]
|
||||
|
||||
async def list_prompts(
|
||||
self,
|
||||
connection: McpSourceConnection,
|
||||
auth: AuthRecord | None,
|
||||
) -> list[DiscoveredPrompt]:
|
||||
async with self._session(connection, auth) as session:
|
||||
result: ListPromptsResult = await session.list_prompts()
|
||||
return [prompt_to_discovered(prompt) for prompt in result.prompts]
|
||||
|
||||
async def get_connection_metadata(
|
||||
self,
|
||||
connection: McpSourceConnection,
|
||||
auth: AuthRecord | None,
|
||||
) -> dict[str, Any]:
|
||||
transport = connection.transport
|
||||
return {
|
||||
"server": connection.provider,
|
||||
"transport": transport.kind if transport is not None else None,
|
||||
}
|
||||
|
||||
async def read_resource(
|
||||
self,
|
||||
connection: McpSourceConnection,
|
||||
auth: AuthRecord | None,
|
||||
uri: str,
|
||||
) -> dict[str, Any]:
|
||||
async with self._session(connection, auth) as session:
|
||||
result = await session.read_resource(AnyUrl(uri))
|
||||
return result.model_dump(by_alias=True, mode="json", exclude_none=True)
|
||||
|
||||
async def get_prompt(
|
||||
self,
|
||||
connection: McpSourceConnection,
|
||||
auth: AuthRecord | None,
|
||||
prompt_name: str,
|
||||
arguments: dict[str, str] | None = None,
|
||||
) -> dict[str, Any]:
|
||||
async with self._session(connection, auth) as session:
|
||||
result = await session.get_prompt(prompt_name, arguments)
|
||||
return result.model_dump(by_alias=True, mode="json", exclude_none=True)
|
||||
|
||||
async def invoke_method(
|
||||
self,
|
||||
connection: McpSourceConnection,
|
||||
auth: AuthRecord | None,
|
||||
method: str,
|
||||
params: dict[str, Any] | None = None,
|
||||
) -> dict[str, Any]:
|
||||
async with self._session(connection, auth) as session:
|
||||
result = await session.send_request(
|
||||
ClientRequest.model_validate({"method": method, "params": params}),
|
||||
ClientResult,
|
||||
)
|
||||
return result.model_dump(by_alias=True, mode="json", exclude_none=True)
|
||||
|
||||
async def send_notification(
|
||||
self,
|
||||
connection: McpSourceConnection,
|
||||
auth: AuthRecord | None,
|
||||
method: str,
|
||||
params: dict[str, Any] | None = None,
|
||||
) -> None:
|
||||
async with self._session(connection, auth) as session:
|
||||
await session.send_notification(
|
||||
ClientNotification.model_validate({"method": method, "params": params})
|
||||
)
|
||||
|
||||
async def call_tool(
|
||||
self,
|
||||
connection: McpSourceConnection,
|
||||
auth: AuthRecord | None,
|
||||
tool_name: str,
|
||||
payload: dict[str, Any],
|
||||
) -> ToolCallResult:
|
||||
async with self._session(connection, auth) as session:
|
||||
result = await session.call_tool(tool_name, payload)
|
||||
return tool_result_to_call_result(result)
|
||||
__all__ = ["McpSdkAdapter"]
|
||||
|
||||
@@ -7,6 +7,7 @@ from fastmcp import FastMCP
|
||||
from fastmcp.client import Client
|
||||
from fastmcp.client.transports.memory import FastMCPTransport
|
||||
|
||||
from wf_sources_mcp.sdk import McpSdkAdapter
|
||||
from wf_sources_mcp.source_registry import FileSourceRegistryStore
|
||||
|
||||
from ..admin_surface import register_service_admin_tools
|
||||
@@ -15,7 +16,6 @@ from ..broker.transport import normalize_transport
|
||||
from ..documentation import build_local_documentation_source
|
||||
from ..models import BrokerConfig
|
||||
from ..proxy.runtime import ProxyRuntime
|
||||
from ..sdk import McpSdkAdapter
|
||||
from ..workflow_surface import register_workflow_tools
|
||||
from .prompts import register_documentation_prompts
|
||||
from .resources import register_documentation_resources
|
||||
|
||||
Reference in New Issue
Block a user