refactor: create the types subpackage, and the jsonrpc module (#1918)
This commit is contained in:
parent
ae7793efd6
commit
3bcdc1763f
4 changed files with 503 additions and 75 deletions
|
|
@ -42,7 +42,7 @@ from typing import Any, TypeVar, overload
|
|||
import anyio
|
||||
import anyio.lowlevel
|
||||
from anyio.streams.memory import MemoryObjectReceiveStream, MemoryObjectSendStream
|
||||
from pydantic import AnyUrl
|
||||
from pydantic import AnyUrl, TypeAdapter
|
||||
|
||||
import mcp.types as types
|
||||
from mcp.server.experimental.session_features import ExperimentalServerSessionFeatures
|
||||
|
|
@ -105,11 +105,11 @@ class ServerSession(
|
|||
self._exit_stack.push_async_callback(lambda: self._incoming_message_stream_reader.aclose())
|
||||
|
||||
@property
|
||||
def _receive_request_adapter(self) -> types.TypeAdapter[types.ClientRequest]:
|
||||
def _receive_request_adapter(self) -> TypeAdapter[types.ClientRequest]:
|
||||
return types.client_request_adapter
|
||||
|
||||
@property
|
||||
def _receive_notification_adapter(self) -> types.TypeAdapter[types.ClientNotification]:
|
||||
def _receive_notification_adapter(self) -> TypeAdapter[types.ClientNotification]:
|
||||
return types.client_notification_adapter
|
||||
|
||||
@property
|
||||
|
|
|
|||
414
src/mcp/types/__init__.py
Normal file
414
src/mcp/types/__init__.py
Normal file
|
|
@ -0,0 +1,414 @@
|
|||
"""MCP types package."""
|
||||
|
||||
# Re-export everything from _types for backward compatibility
|
||||
from mcp.types._types import (
|
||||
DEFAULT_NEGOTIATED_VERSION,
|
||||
LATEST_PROTOCOL_VERSION,
|
||||
TASK_FORBIDDEN,
|
||||
TASK_OPTIONAL,
|
||||
TASK_REQUIRED,
|
||||
TASK_STATUS_CANCELLED,
|
||||
TASK_STATUS_COMPLETED,
|
||||
TASK_STATUS_FAILED,
|
||||
TASK_STATUS_INPUT_REQUIRED,
|
||||
TASK_STATUS_WORKING,
|
||||
Annotations,
|
||||
AudioContent,
|
||||
BaseMetadata,
|
||||
BlobResourceContents,
|
||||
CallToolRequest,
|
||||
CallToolRequestParams,
|
||||
CallToolResult,
|
||||
CancelledNotification,
|
||||
CancelledNotificationParams,
|
||||
CancelTaskRequest,
|
||||
CancelTaskRequestParams,
|
||||
CancelTaskResult,
|
||||
ClientCapabilities,
|
||||
ClientNotification,
|
||||
ClientRequest,
|
||||
ClientResult,
|
||||
ClientTasksCapability,
|
||||
ClientTasksRequestsCapability,
|
||||
CompleteRequest,
|
||||
CompleteRequestParams,
|
||||
CompleteResult,
|
||||
Completion,
|
||||
CompletionArgument,
|
||||
CompletionContext,
|
||||
CompletionsCapability,
|
||||
ContentBlock,
|
||||
CreateMessageRequest,
|
||||
CreateMessageRequestParams,
|
||||
CreateMessageResult,
|
||||
CreateMessageResultWithTools,
|
||||
CreateTaskResult,
|
||||
Cursor,
|
||||
ElicitationCapability,
|
||||
ElicitationRequiredErrorData,
|
||||
ElicitCompleteNotification,
|
||||
ElicitCompleteNotificationParams,
|
||||
ElicitRequest,
|
||||
ElicitRequestedSchema,
|
||||
ElicitRequestFormParams,
|
||||
ElicitRequestParams,
|
||||
ElicitRequestURLParams,
|
||||
ElicitResult,
|
||||
EmbeddedResource,
|
||||
EmptyResult,
|
||||
FormElicitationCapability,
|
||||
GetPromptRequest,
|
||||
GetPromptRequestParams,
|
||||
GetPromptResult,
|
||||
GetTaskPayloadRequest,
|
||||
GetTaskPayloadRequestParams,
|
||||
GetTaskPayloadResult,
|
||||
GetTaskRequest,
|
||||
GetTaskRequestParams,
|
||||
GetTaskResult,
|
||||
Icon,
|
||||
ImageContent,
|
||||
Implementation,
|
||||
IncludeContext,
|
||||
InitializedNotification,
|
||||
InitializeRequest,
|
||||
InitializeRequestParams,
|
||||
InitializeResult,
|
||||
ListPromptsRequest,
|
||||
ListPromptsResult,
|
||||
ListResourcesRequest,
|
||||
ListResourcesResult,
|
||||
ListResourceTemplatesRequest,
|
||||
ListResourceTemplatesResult,
|
||||
ListRootsRequest,
|
||||
ListRootsResult,
|
||||
ListTasksRequest,
|
||||
ListTasksResult,
|
||||
ListToolsRequest,
|
||||
ListToolsResult,
|
||||
LoggingCapability,
|
||||
LoggingLevel,
|
||||
LoggingMessageNotification,
|
||||
LoggingMessageNotificationParams,
|
||||
MCPModel,
|
||||
MethodT,
|
||||
ModelHint,
|
||||
ModelPreferences,
|
||||
Notification,
|
||||
NotificationParams,
|
||||
NotificationParamsT,
|
||||
PaginatedRequest,
|
||||
PaginatedRequestParams,
|
||||
PaginatedResult,
|
||||
PingRequest,
|
||||
ProgressNotification,
|
||||
ProgressNotificationParams,
|
||||
ProgressToken,
|
||||
Prompt,
|
||||
PromptArgument,
|
||||
PromptListChangedNotification,
|
||||
PromptMessage,
|
||||
PromptReference,
|
||||
PromptsCapability,
|
||||
ReadResourceRequest,
|
||||
ReadResourceRequestParams,
|
||||
ReadResourceResult,
|
||||
RelatedTaskMetadata,
|
||||
Request,
|
||||
RequestParams,
|
||||
RequestParamsT,
|
||||
Resource,
|
||||
ResourceContents,
|
||||
ResourceLink,
|
||||
ResourceListChangedNotification,
|
||||
ResourcesCapability,
|
||||
ResourceTemplate,
|
||||
ResourceTemplateReference,
|
||||
ResourceUpdatedNotification,
|
||||
ResourceUpdatedNotificationParams,
|
||||
Result,
|
||||
Role,
|
||||
Root,
|
||||
RootsCapability,
|
||||
RootsListChangedNotification,
|
||||
SamplingCapability,
|
||||
SamplingContent,
|
||||
SamplingContextCapability,
|
||||
SamplingMessage,
|
||||
SamplingMessageContentBlock,
|
||||
SamplingToolsCapability,
|
||||
ServerCapabilities,
|
||||
ServerNotification,
|
||||
ServerRequest,
|
||||
ServerResult,
|
||||
ServerTasksCapability,
|
||||
ServerTasksRequestsCapability,
|
||||
SetLevelRequest,
|
||||
SetLevelRequestParams,
|
||||
StopReason,
|
||||
SubscribeRequest,
|
||||
SubscribeRequestParams,
|
||||
Task,
|
||||
TaskExecutionMode,
|
||||
TaskMetadata,
|
||||
TasksCallCapability,
|
||||
TasksCancelCapability,
|
||||
TasksCreateElicitationCapability,
|
||||
TasksCreateMessageCapability,
|
||||
TasksElicitationCapability,
|
||||
TasksListCapability,
|
||||
TasksSamplingCapability,
|
||||
TaskStatus,
|
||||
TaskStatusNotification,
|
||||
TaskStatusNotificationParams,
|
||||
TasksToolsCapability,
|
||||
TextContent,
|
||||
TextResourceContents,
|
||||
Tool,
|
||||
ToolAnnotations,
|
||||
ToolChoice,
|
||||
ToolExecution,
|
||||
ToolListChangedNotification,
|
||||
ToolResultContent,
|
||||
ToolsCapability,
|
||||
ToolUseContent,
|
||||
UnsubscribeRequest,
|
||||
UnsubscribeRequestParams,
|
||||
UrlElicitationCapability,
|
||||
client_notification_adapter,
|
||||
client_request_adapter,
|
||||
client_result_adapter,
|
||||
server_notification_adapter,
|
||||
server_request_adapter,
|
||||
server_result_adapter,
|
||||
)
|
||||
|
||||
# Re-export JSONRPC types
|
||||
from mcp.types.jsonrpc import (
|
||||
CONNECTION_CLOSED,
|
||||
INTERNAL_ERROR,
|
||||
INVALID_PARAMS,
|
||||
INVALID_REQUEST,
|
||||
METHOD_NOT_FOUND,
|
||||
PARSE_ERROR,
|
||||
URL_ELICITATION_REQUIRED,
|
||||
ErrorData,
|
||||
JSONRPCError,
|
||||
JSONRPCMessage,
|
||||
JSONRPCNotification,
|
||||
JSONRPCRequest,
|
||||
JSONRPCResponse,
|
||||
RequestId,
|
||||
jsonrpc_message_adapter,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
# Protocol version constants
|
||||
"LATEST_PROTOCOL_VERSION",
|
||||
"DEFAULT_NEGOTIATED_VERSION",
|
||||
# Task execution mode constants
|
||||
"TASK_FORBIDDEN",
|
||||
"TASK_OPTIONAL",
|
||||
"TASK_REQUIRED",
|
||||
# Task status constants
|
||||
"TASK_STATUS_CANCELLED",
|
||||
"TASK_STATUS_COMPLETED",
|
||||
"TASK_STATUS_FAILED",
|
||||
"TASK_STATUS_INPUT_REQUIRED",
|
||||
"TASK_STATUS_WORKING",
|
||||
# Type aliases and variables
|
||||
"ContentBlock",
|
||||
"Cursor",
|
||||
"ElicitRequestedSchema",
|
||||
"ElicitRequestParams",
|
||||
"IncludeContext",
|
||||
"LoggingLevel",
|
||||
"MethodT",
|
||||
"NotificationParamsT",
|
||||
"ProgressToken",
|
||||
"RequestParamsT",
|
||||
"Role",
|
||||
"SamplingContent",
|
||||
"SamplingMessageContentBlock",
|
||||
"StopReason",
|
||||
"TaskExecutionMode",
|
||||
"TaskStatus",
|
||||
# Base classes
|
||||
"MCPModel",
|
||||
"BaseMetadata",
|
||||
"Request",
|
||||
"Notification",
|
||||
"Result",
|
||||
"RequestParams",
|
||||
"NotificationParams",
|
||||
"PaginatedRequest",
|
||||
"PaginatedRequestParams",
|
||||
"PaginatedResult",
|
||||
"EmptyResult",
|
||||
# Capabilities
|
||||
"ClientCapabilities",
|
||||
"ClientTasksCapability",
|
||||
"ClientTasksRequestsCapability",
|
||||
"CompletionsCapability",
|
||||
"ElicitationCapability",
|
||||
"FormElicitationCapability",
|
||||
"LoggingCapability",
|
||||
"PromptsCapability",
|
||||
"ResourcesCapability",
|
||||
"RootsCapability",
|
||||
"SamplingCapability",
|
||||
"SamplingContextCapability",
|
||||
"SamplingToolsCapability",
|
||||
"ServerCapabilities",
|
||||
"ServerTasksCapability",
|
||||
"ServerTasksRequestsCapability",
|
||||
"TasksCancelCapability",
|
||||
"TasksCallCapability",
|
||||
"TasksCreateElicitationCapability",
|
||||
"TasksCreateMessageCapability",
|
||||
"TasksElicitationCapability",
|
||||
"TasksListCapability",
|
||||
"TasksSamplingCapability",
|
||||
"TasksToolsCapability",
|
||||
"ToolsCapability",
|
||||
"UrlElicitationCapability",
|
||||
# Content types
|
||||
"Annotations",
|
||||
"AudioContent",
|
||||
"BlobResourceContents",
|
||||
"EmbeddedResource",
|
||||
"Icon",
|
||||
"ImageContent",
|
||||
"ResourceContents",
|
||||
"ResourceLink",
|
||||
"TextContent",
|
||||
"TextResourceContents",
|
||||
"ToolResultContent",
|
||||
"ToolUseContent",
|
||||
# Entity types
|
||||
"Completion",
|
||||
"CompletionArgument",
|
||||
"CompletionContext",
|
||||
"Implementation",
|
||||
"ModelHint",
|
||||
"ModelPreferences",
|
||||
"Prompt",
|
||||
"PromptArgument",
|
||||
"PromptMessage",
|
||||
"PromptReference",
|
||||
"Resource",
|
||||
"ResourceTemplate",
|
||||
"ResourceTemplateReference",
|
||||
"Root",
|
||||
"SamplingMessage",
|
||||
"Task",
|
||||
"TaskMetadata",
|
||||
"RelatedTaskMetadata",
|
||||
"Tool",
|
||||
"ToolAnnotations",
|
||||
"ToolChoice",
|
||||
"ToolExecution",
|
||||
# Requests
|
||||
"CallToolRequest",
|
||||
"CallToolRequestParams",
|
||||
"CancelTaskRequest",
|
||||
"CancelTaskRequestParams",
|
||||
"CompleteRequest",
|
||||
"CompleteRequestParams",
|
||||
"CreateMessageRequest",
|
||||
"CreateMessageRequestParams",
|
||||
"ElicitRequest",
|
||||
"ElicitRequestFormParams",
|
||||
"ElicitRequestURLParams",
|
||||
"GetPromptRequest",
|
||||
"GetPromptRequestParams",
|
||||
"GetTaskPayloadRequest",
|
||||
"GetTaskPayloadRequestParams",
|
||||
"GetTaskRequest",
|
||||
"GetTaskRequestParams",
|
||||
"InitializeRequest",
|
||||
"InitializeRequestParams",
|
||||
"ListPromptsRequest",
|
||||
"ListResourcesRequest",
|
||||
"ListResourceTemplatesRequest",
|
||||
"ListRootsRequest",
|
||||
"ListTasksRequest",
|
||||
"ListToolsRequest",
|
||||
"PingRequest",
|
||||
"ReadResourceRequest",
|
||||
"ReadResourceRequestParams",
|
||||
"SetLevelRequest",
|
||||
"SetLevelRequestParams",
|
||||
"SubscribeRequest",
|
||||
"SubscribeRequestParams",
|
||||
"UnsubscribeRequest",
|
||||
"UnsubscribeRequestParams",
|
||||
# Results
|
||||
"CallToolResult",
|
||||
"CancelTaskResult",
|
||||
"CompleteResult",
|
||||
"CreateMessageResult",
|
||||
"CreateMessageResultWithTools",
|
||||
"CreateTaskResult",
|
||||
"ElicitResult",
|
||||
"ElicitationRequiredErrorData",
|
||||
"GetPromptResult",
|
||||
"GetTaskPayloadResult",
|
||||
"GetTaskResult",
|
||||
"InitializeResult",
|
||||
"ListPromptsResult",
|
||||
"ListResourcesResult",
|
||||
"ListResourceTemplatesResult",
|
||||
"ListRootsResult",
|
||||
"ListTasksResult",
|
||||
"ListToolsResult",
|
||||
"ReadResourceResult",
|
||||
# Notifications
|
||||
"CancelledNotification",
|
||||
"CancelledNotificationParams",
|
||||
"ElicitCompleteNotification",
|
||||
"ElicitCompleteNotificationParams",
|
||||
"InitializedNotification",
|
||||
"LoggingMessageNotification",
|
||||
"LoggingMessageNotificationParams",
|
||||
"ProgressNotification",
|
||||
"ProgressNotificationParams",
|
||||
"PromptListChangedNotification",
|
||||
"ResourceListChangedNotification",
|
||||
"ResourceUpdatedNotification",
|
||||
"ResourceUpdatedNotificationParams",
|
||||
"RootsListChangedNotification",
|
||||
"TaskStatusNotification",
|
||||
"TaskStatusNotificationParams",
|
||||
"ToolListChangedNotification",
|
||||
# Union types for request/response routing
|
||||
"ClientNotification",
|
||||
"ClientRequest",
|
||||
"ClientResult",
|
||||
"ServerNotification",
|
||||
"ServerRequest",
|
||||
"ServerResult",
|
||||
# Type adapters
|
||||
"client_notification_adapter",
|
||||
"client_request_adapter",
|
||||
"client_result_adapter",
|
||||
"server_notification_adapter",
|
||||
"server_request_adapter",
|
||||
"server_result_adapter",
|
||||
# JSON-RPC types
|
||||
"CONNECTION_CLOSED",
|
||||
"INTERNAL_ERROR",
|
||||
"INVALID_PARAMS",
|
||||
"INVALID_REQUEST",
|
||||
"METHOD_NOT_FOUND",
|
||||
"PARSE_ERROR",
|
||||
"URL_ELICITATION_REQUIRED",
|
||||
"ErrorData",
|
||||
"JSONRPCError",
|
||||
"JSONRPCMessage",
|
||||
"JSONRPCNotification",
|
||||
"JSONRPCRequest",
|
||||
"JSONRPCResponse",
|
||||
"RequestId",
|
||||
"jsonrpc_message_adapter",
|
||||
]
|
||||
1745
src/mcp/types/_types.py
Normal file
1745
src/mcp/types/_types.py
Normal file
File diff suppressed because it is too large
Load diff
83
src/mcp/types/jsonrpc.py
Normal file
83
src/mcp/types/jsonrpc.py
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
"""This module follows the JSON-RPC 2.0 specification: https://www.jsonrpc.org/specification."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Annotated, Any, Literal
|
||||
|
||||
from pydantic import BaseModel, Field, TypeAdapter
|
||||
|
||||
RequestId = Annotated[int, Field(strict=True)] | str
|
||||
"""The ID of a JSON-RPC request."""
|
||||
|
||||
|
||||
class JSONRPCRequest(BaseModel):
|
||||
"""A JSON-RPC request that expects a response."""
|
||||
|
||||
jsonrpc: Literal["2.0"]
|
||||
id: RequestId
|
||||
method: str
|
||||
params: dict[str, Any] | None = None
|
||||
|
||||
|
||||
class JSONRPCNotification(BaseModel):
|
||||
"""A JSON-RPC notification which does not expect a response."""
|
||||
|
||||
jsonrpc: Literal["2.0"]
|
||||
method: str
|
||||
params: dict[str, Any] | None = None
|
||||
|
||||
|
||||
# TODO(Marcelo): This is actually not correct. A JSONRPCResponse is the union of a successful response and an error.
|
||||
class JSONRPCResponse(BaseModel):
|
||||
"""A successful (non-error) response to a request."""
|
||||
|
||||
jsonrpc: Literal["2.0"]
|
||||
id: RequestId
|
||||
result: dict[str, Any]
|
||||
|
||||
|
||||
# MCP-specific error codes in the range [-32000, -32099]
|
||||
URL_ELICITATION_REQUIRED = -32042
|
||||
"""Error code indicating that a URL mode elicitation is required before the request can be processed."""
|
||||
|
||||
# SDK error codes
|
||||
CONNECTION_CLOSED = -32000
|
||||
# REQUEST_TIMEOUT = -32001 # the typescript sdk uses this
|
||||
|
||||
# Standard JSON-RPC error codes
|
||||
PARSE_ERROR = -32700
|
||||
INVALID_REQUEST = -32600
|
||||
METHOD_NOT_FOUND = -32601
|
||||
INVALID_PARAMS = -32602
|
||||
INTERNAL_ERROR = -32603
|
||||
|
||||
|
||||
class ErrorData(BaseModel):
|
||||
"""Error information for JSON-RPC error responses."""
|
||||
|
||||
code: int
|
||||
"""The error type that occurred."""
|
||||
|
||||
message: str
|
||||
"""
|
||||
A short description of the error. The message SHOULD be limited to a concise single
|
||||
sentence.
|
||||
"""
|
||||
|
||||
data: Any = None
|
||||
"""
|
||||
Additional information about the error. The value of this member is defined by the
|
||||
sender (e.g. detailed error information, nested errors etc.).
|
||||
"""
|
||||
|
||||
|
||||
class JSONRPCError(BaseModel):
|
||||
"""A response to a request that indicates an error occurred."""
|
||||
|
||||
jsonrpc: Literal["2.0"]
|
||||
id: str | int
|
||||
error: ErrorData
|
||||
|
||||
|
||||
JSONRPCMessage = JSONRPCRequest | JSONRPCNotification | JSONRPCResponse | JSONRPCError
|
||||
jsonrpc_message_adapter: TypeAdapter[JSONRPCMessage] = TypeAdapter(JSONRPCMessage)
|
||||
Loading…
Reference in a new issue