Update convenience methods on ClientSession and ServerSession

This commit is contained in:
Justin Spahr-Summers
2024-11-06 12:24:53 +00:00
parent 1634343931
commit 4ac03d40f9
2 changed files with 133 additions and 3 deletions

View File

@@ -12,7 +12,7 @@ from mcp_python.shared.session import (
RequestResponder,
)
from mcp_python.types import (
LATEST_PROTOCOL_VERSION,
ListRootsResult, LATEST_PROTOCOL_VERSION,
ClientNotification,
ClientRequest,
CreateMessageResult,
@@ -28,6 +28,10 @@ from mcp_python.types import (
ServerNotification,
ServerRequest,
ServerResult,
ResourceListChangedNotification,
ToolListChangedNotification,
PromptListChangedNotification,
ModelPreferences,
)
@@ -142,6 +146,7 @@ class ServerSession(
temperature: float | None = None,
stop_sequences: list[str] | None = None,
metadata: dict[str, Any] | None = None,
model_preferences: ModelPreferences | None = None,
) -> CreateMessageResult:
"""Send a sampling/create_message request."""
from mcp_python.types import (
@@ -161,12 +166,26 @@ class ServerSession(
maxTokens=max_tokens,
stopSequences=stop_sequences,
metadata=metadata,
modelPreferences=model_preferences,
),
)
),
CreateMessageResult,
)
async def list_roots(self) -> ListRootsResult:
"""Send a roots/list request."""
from mcp_python.types import ListRootsRequest
return await self.send_request(
ServerRequest(
ListRootsRequest(
method="roots/list",
)
),
ListRootsResult,
)
async def send_ping(self) -> EmptyResult:
"""Send a ping request."""
from mcp_python.types import PingRequest
@@ -198,3 +217,33 @@ class ServerSession(
)
)
)
async def send_resource_list_changed(self) -> None:
"""Send a resource list changed notification."""
await self.send_notification(
ServerNotification(
ResourceListChangedNotification(
method="notifications/resources/list_changed",
)
)
)
async def send_tool_list_changed(self) -> None:
"""Send a tool list changed notification."""
await self.send_notification(
ServerNotification(
ToolListChangedNotification(
method="notifications/tools/list_changed",
)
)
)
async def send_prompt_list_changed(self) -> None:
"""Send a prompt list changed notification."""
await self.send_notification(
ServerNotification(
PromptListChangedNotification(
method="notifications/prompts/list_changed",
)
)
)