Skip to content

Commit 5c052c1

Browse files
committed
add transport abstraction
1 parent 3390e49 commit 5c052c1

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
from abc import ABC
2+
from abc import abstractmethod
3+
from datetime import timedelta
4+
5+
from typing import Any
6+
7+
from pydantic import AnyUrl
8+
9+
from mcp import types
10+
from mcp.shared.session import ProgressFnT
11+
12+
13+
class TransportSession(ABC):
14+
"""Abstract base class for communication transports."""
15+
16+
@abstractmethod
17+
async def initialize(self) -> types.InitializeResult:
18+
"""Send an initialize request."""
19+
...
20+
21+
@abstractmethod
22+
async def send_ping(self):
23+
...
24+
25+
@abstractmethod
26+
async def send_progress_notification(
27+
self,
28+
progress_token: str | int,
29+
progress: float,
30+
total: float | None = None,
31+
message: str | None = None,
32+
) -> None:
33+
...
34+
35+
@abstractmethod
36+
async def set_logging_level(
37+
self,
38+
level: types.LoggingLevel,
39+
) -> types.EmptyResult:
40+
"""Send a logging/setLevel request."""
41+
...
42+
43+
@abstractmethod
44+
async def list_resources(
45+
self,
46+
cursor: str | None = None,
47+
) -> types.ListResourcesResult:
48+
"""Send a resources/list request."""
49+
...
50+
51+
@abstractmethod
52+
async def list_resource_templates(
53+
self,
54+
cursor: str | None = None,
55+
) -> types.ListResourceTemplatesResult:
56+
"""Send a resources/templates/list request."""
57+
...
58+
59+
@abstractmethod
60+
async def read_resource(self, uri: AnyUrl) -> types.ReadResourceResult:
61+
"""Send a resources/read request."""
62+
...
63+
64+
@abstractmethod
65+
async def subscribe_resource(self, uri: AnyUrl) -> types.EmptyResult:
66+
"""Send a resources/subscribe request."""
67+
...
68+
69+
@abstractmethod
70+
async def unsubscribe_resource(self, uri: AnyUrl) -> types.EmptyResult:
71+
"""Send a resources/unsubscribe request."""
72+
...
73+
74+
@abstractmethod
75+
async def call_tool(
76+
self,
77+
name: str,
78+
arguments: Any | None = None,
79+
read_timeout_seconds: timedelta | None = None,
80+
progress_callback: ProgressFnT | None = None,
81+
) -> types.CallToolResult:
82+
"""Send a tools/call request with optional progress callback support."""
83+
...
84+
85+
@abstractmethod
86+
async def _validate_tool_result(
87+
self,
88+
name: str,
89+
result: types.CallToolResult,
90+
) -> None:
91+
"""Validate the structured content of a tool result against its output
92+
schema."""
93+
...
94+
95+
@abstractmethod
96+
async def list_prompts(
97+
self,
98+
cursor: str | None = None,
99+
) -> types.ListPromptsResult:
100+
"""Send a prompts/list request."""
101+
...
102+
103+
@abstractmethod
104+
async def get_prompt(
105+
self,
106+
name: str,
107+
arguments: dict[str, str] | None = None,
108+
) -> types.GetPromptResult:
109+
"""Send a prompts/get request."""
110+
...
111+
112+
@abstractmethod
113+
async def complete(
114+
self,
115+
ref: types.ResourceTemplateReference | types.PromptReference,
116+
argument: dict[str, str],
117+
context_arguments: dict[str, str] | None = None,
118+
) -> types.CompleteResult:
119+
"""Send a completion/complete request."""
120+
...
121+
122+
@abstractmethod
123+
async def list_tools(
124+
self,
125+
cursor: str | None = None,
126+
) -> types.ListToolsResult:
127+
"""Send a tools/list request."""
128+
...
129+
130+
@abstractmethod
131+
async def send_roots_list_changed(self) -> None:
132+
"""Send a roots/list_changed notification."""
133+
...

0 commit comments

Comments
 (0)