Skip to content

Commit 5083654

Browse files
committed
add transport session for server
1 parent 6538db0 commit 5083654

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
"""Abstract base class for transport sessions."""
2+
3+
import abc
4+
from typing import Any
5+
6+
from anyio.streams.memory import MemoryObjectReceiveStream
7+
from pydantic import AnyUrl
8+
9+
import mcp_grpc.types as types
10+
from mcp_grpc.server.session import ServerRequestResponder
11+
12+
13+
class TransportSession(abc.ABC):
14+
"""Abstract base class for transport sessions."""
15+
16+
@property
17+
@abc.abstractmethod
18+
def client_params(self) -> types.InitializeRequestParams | None:
19+
"""Client initialization parameters."""
20+
raise NotImplementedError
21+
22+
@abc.abstractmethod
23+
def check_client_capability(self, capability: types.ClientCapabilities) -> bool:
24+
"""Check if the client supports a specific capability."""
25+
raise NotImplementedError
26+
27+
@abc.abstractmethod
28+
async def send_log_message(
29+
self,
30+
level: types.LoggingLevel,
31+
data: Any,
32+
logger: str | None = None,
33+
related_request_id: types.RequestId | None = None,
34+
) -> None:
35+
"""Send a log message notification."""
36+
raise NotImplementedError
37+
38+
@abc.abstractmethod
39+
async def send_resource_updated(self, uri: AnyUrl) -> None:
40+
"""Send a resource updated notification."""
41+
raise NotImplementedError
42+
43+
@abc.abstractmethod
44+
async def create_message(
45+
self,
46+
messages: list[types.SamplingMessage],
47+
*,
48+
max_tokens: int,
49+
system_prompt: str | None = None,
50+
include_context: types.IncludeContext | None = None,
51+
temperature: float | None = None,
52+
stop_sequences: list[str] | None = None,
53+
metadata: dict[str, Any] | None = None,
54+
model_preferences: types.ModelPreferences | None = None,
55+
related_request_id: types.RequestId | None = None,
56+
) -> types.CreateMessageResult:
57+
"""Send a sampling/create_message request."""
58+
raise NotImplementedError
59+
60+
@abc.abstractmethod
61+
async def list_roots(self) -> types.ListRootsResult:
62+
"""Send a roots/list request."""
63+
raise NotImplementedError
64+
65+
@abc.abstractmethod
66+
async def elicit(
67+
self,
68+
message: str,
69+
requestedSchema: types.ElicitRequestedSchema,
70+
related_request_id: types.RequestId | None = None,
71+
) -> types.ElicitResult:
72+
"""Send an elicitation/create request."""
73+
raise NotImplementedError
74+
75+
@abc.abstractmethod
76+
async def send_ping(self) -> types.EmptyResult:
77+
"""Send a ping request."""
78+
raise NotImplementedError
79+
80+
@abc.abstractmethod
81+
async def send_progress_notification(
82+
self,
83+
progress_token: str | int,
84+
progress: float,
85+
total: float | None = None,
86+
message: str | None = None,
87+
related_request_id: str | None = None,
88+
) -> None:
89+
"""Send a progress notification."""
90+
raise NotImplementedError
91+
92+
@abc.abstractmethod
93+
async def send_resource_list_changed(self) -> None:
94+
"""Send a resource list changed notification."""
95+
raise NotImplementedError
96+
97+
@abc.abstractmethod
98+
async def send_tool_list_changed(self) -> None:
99+
"""Send a tool list changed notification."""
100+
raise NotImplementedError
101+
102+
@abc.abstractmethod
103+
async def send_prompt_list_changed(self) -> None:
104+
"""Send a prompt list changed notification."""
105+
raise NotImplementedError
106+
107+
@property
108+
@abc.abstractmethod
109+
def incoming_messages(
110+
self,
111+
) -> MemoryObjectReceiveStream[ServerRequestResponder]:
112+
"""Incoming messages stream."""
113+
raise NotImplementedError

0 commit comments

Comments
 (0)