Skip to content

Commit 68457a1

Browse files
committed
types: Setting default value for method: Literal
This makes object creation simple, as the method would be automatically set.
1 parent 09e3a05 commit 68457a1

File tree

2 files changed

+57
-26
lines changed

2 files changed

+57
-26
lines changed

src/mcp/types.py

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ class InitializeRequest(Request[InitializeRequestParams, Literal["initialize"]])
326326
to begin initialization.
327327
"""
328328

329-
method: Literal["initialize"]
329+
method: Literal["initialize"] = "initialize"
330330
params: InitializeRequestParams
331331

332332

@@ -347,7 +347,7 @@ class InitializedNotification(Notification[NotificationParams | None, Literal["n
347347
finished.
348348
"""
349349

350-
method: Literal["notifications/initialized"]
350+
method: Literal["notifications/initialized"] = "notifications/initialized"
351351
params: NotificationParams | None = None
352352

353353

@@ -357,7 +357,7 @@ class PingRequest(Request[RequestParams | None, Literal["ping"]]):
357357
still alive.
358358
"""
359359

360-
method: Literal["ping"]
360+
method: Literal["ping"] = "ping"
361361
params: RequestParams | None = None
362362

363363

@@ -390,14 +390,14 @@ class ProgressNotification(Notification[ProgressNotificationParams, Literal["not
390390
long-running request.
391391
"""
392392

393-
method: Literal["notifications/progress"]
393+
method: Literal["notifications/progress"] = "notifications/progress"
394394
params: ProgressNotificationParams
395395

396396

397397
class ListResourcesRequest(PaginatedRequest[Literal["resources/list"]]):
398398
"""Sent from the client to request a list of resources the server has."""
399399

400-
method: Literal["resources/list"]
400+
method: Literal["resources/list"] = "resources/list"
401401

402402

403403
class Annotations(BaseModel):
@@ -464,7 +464,7 @@ class ListResourcesResult(PaginatedResult):
464464
class ListResourceTemplatesRequest(PaginatedRequest[Literal["resources/templates/list"]]):
465465
"""Sent from the client to request a list of resource templates the server has."""
466466

467-
method: Literal["resources/templates/list"]
467+
method: Literal["resources/templates/list"] = "resources/templates/list"
468468

469469

470470
class ListResourceTemplatesResult(PaginatedResult):
@@ -487,7 +487,7 @@ class ReadResourceRequestParams(RequestParams):
487487
class ReadResourceRequest(Request[ReadResourceRequestParams, Literal["resources/read"]]):
488488
"""Sent from the client to the server, to read a specific resource URI."""
489489

490-
method: Literal["resources/read"]
490+
method: Literal["resources/read"] = "resources/read"
491491
params: ReadResourceRequestParams
492492

493493

@@ -537,7 +537,7 @@ class ResourceListChangedNotification(
537537
of resources it can read from has changed.
538538
"""
539539

540-
method: Literal["notifications/resources/list_changed"]
540+
method: Literal["notifications/resources/list_changed"] = "notifications/resources/list_changed"
541541
params: NotificationParams | None = None
542542

543543

@@ -558,7 +558,7 @@ class SubscribeRequest(Request[SubscribeRequestParams, Literal["resources/subscr
558558
whenever a particular resource changes.
559559
"""
560560

561-
method: Literal["resources/subscribe"]
561+
method: Literal["resources/subscribe"] = "resources/subscribe"
562562
params: SubscribeRequestParams
563563

564564

@@ -576,7 +576,7 @@ class UnsubscribeRequest(Request[UnsubscribeRequestParams, Literal["resources/un
576576
the server.
577577
"""
578578

579-
method: Literal["resources/unsubscribe"]
579+
method: Literal["resources/unsubscribe"] = "resources/unsubscribe"
580580
params: UnsubscribeRequestParams
581581

582582

@@ -599,14 +599,14 @@ class ResourceUpdatedNotification(
599599
changed and may need to be read again.
600600
"""
601601

602-
method: Literal["notifications/resources/updated"]
602+
method: Literal["notifications/resources/updated"] = "notifications/resources/updated"
603603
params: ResourceUpdatedNotificationParams
604604

605605

606606
class ListPromptsRequest(PaginatedRequest[Literal["prompts/list"]]):
607607
"""Sent from the client to request a list of prompts and prompt templates."""
608608

609-
method: Literal["prompts/list"]
609+
method: Literal["prompts/list"] = "prompts/list"
610610

611611

612612
class PromptArgument(BaseModel):
@@ -655,7 +655,7 @@ class GetPromptRequestParams(RequestParams):
655655
class GetPromptRequest(Request[GetPromptRequestParams, Literal["prompts/get"]]):
656656
"""Used by the client to get a prompt provided by the server."""
657657

658-
method: Literal["prompts/get"]
658+
method: Literal["prompts/get"] = "prompts/get"
659659
params: GetPromptRequestParams
660660

661661

@@ -782,14 +782,14 @@ class PromptListChangedNotification(
782782
of prompts it offers has changed.
783783
"""
784784

785-
method: Literal["notifications/prompts/list_changed"]
785+
method: Literal["notifications/prompts/list_changed"] = "notifications/prompts/list_changed"
786786
params: NotificationParams | None = None
787787

788788

789789
class ListToolsRequest(PaginatedRequest[Literal["tools/list"]]):
790790
"""Sent from the client to request a list of tools the server has."""
791791

792-
method: Literal["tools/list"]
792+
method: Literal["tools/list"] = "tools/list"
793793

794794

795795
class ToolAnnotations(BaseModel):
@@ -879,7 +879,7 @@ class CallToolRequestParams(RequestParams):
879879
class CallToolRequest(Request[CallToolRequestParams, Literal["tools/call"]]):
880880
"""Used by the client to invoke a tool provided by the server."""
881881

882-
method: Literal["tools/call"]
882+
method: Literal["tools/call"] = "tools/call"
883883
params: CallToolRequestParams
884884

885885

@@ -898,7 +898,7 @@ class ToolListChangedNotification(Notification[NotificationParams | None, Litera
898898
of tools it offers has changed.
899899
"""
900900

901-
method: Literal["notifications/tools/list_changed"]
901+
method: Literal["notifications/tools/list_changed"] = "notifications/tools/list_changed"
902902
params: NotificationParams | None = None
903903

904904

@@ -916,7 +916,7 @@ class SetLevelRequestParams(RequestParams):
916916
class SetLevelRequest(Request[SetLevelRequestParams, Literal["logging/setLevel"]]):
917917
"""A request from the client to the server, to enable or adjust logging."""
918918

919-
method: Literal["logging/setLevel"]
919+
method: Literal["logging/setLevel"] = "logging/setLevel"
920920
params: SetLevelRequestParams
921921

922922

@@ -938,7 +938,7 @@ class LoggingMessageNotificationParams(NotificationParams):
938938
class LoggingMessageNotification(Notification[LoggingMessageNotificationParams, Literal["notifications/message"]]):
939939
"""Notification of a log message passed from server to client."""
940940

941-
method: Literal["notifications/message"]
941+
method: Literal["notifications/message"] = "notifications/message"
942942
params: LoggingMessageNotificationParams
943943

944944

@@ -1033,7 +1033,7 @@ class CreateMessageRequestParams(RequestParams):
10331033
class CreateMessageRequest(Request[CreateMessageRequestParams, Literal["sampling/createMessage"]]):
10341034
"""A request from the server to sample an LLM via the client."""
10351035

1036-
method: Literal["sampling/createMessage"]
1036+
method: Literal["sampling/createMessage"] = "sampling/createMessage"
10371037
params: CreateMessageRequestParams
10381038

10391039

@@ -1105,7 +1105,7 @@ class CompleteRequestParams(RequestParams):
11051105
class CompleteRequest(Request[CompleteRequestParams, Literal["completion/complete"]]):
11061106
"""A request from the client to the server, to ask for completion options."""
11071107

1108-
method: Literal["completion/complete"]
1108+
method: Literal["completion/complete"] = "completion/complete"
11091109
params: CompleteRequestParams
11101110

11111111

@@ -1144,7 +1144,7 @@ class ListRootsRequest(Request[RequestParams | None, Literal["roots/list"]]):
11441144
structure or access specific locations that the client has permission to read from.
11451145
"""
11461146

1147-
method: Literal["roots/list"]
1147+
method: Literal["roots/list"] = "roots/list"
11481148
params: RequestParams | None = None
11491149

11501150

@@ -1193,7 +1193,7 @@ class RootsListChangedNotification(
11931193
using the ListRootsRequest.
11941194
"""
11951195

1196-
method: Literal["notifications/roots/list_changed"]
1196+
method: Literal["notifications/roots/list_changed"] = "notifications/roots/list_changed"
11971197
params: NotificationParams | None = None
11981198

11991199

@@ -1213,7 +1213,7 @@ class CancelledNotification(Notification[CancelledNotificationParams, Literal["n
12131213
previously-issued request.
12141214
"""
12151215

1216-
method: Literal["notifications/cancelled"]
1216+
method: Literal["notifications/cancelled"] = "notifications/cancelled"
12171217
params: CancelledNotificationParams
12181218

12191219

@@ -1259,7 +1259,7 @@ class ElicitRequestParams(RequestParams):
12591259
class ElicitRequest(Request[ElicitRequestParams, Literal["elicitation/create"]]):
12601260
"""A request from the server to elicit information from the client."""
12611261

1262-
method: Literal["elicitation/create"]
1262+
method: Literal["elicitation/create"] = "elicitation/create"
12631263
params: ElicitRequestParams
12641264

12651265

tests/test_types.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
import pytest
22

3-
from mcp.types import LATEST_PROTOCOL_VERSION, ClientRequest, JSONRPCMessage, JSONRPCRequest
3+
from mcp.types import (
4+
LATEST_PROTOCOL_VERSION,
5+
ClientCapabilities,
6+
ClientRequest,
7+
Implementation,
8+
InitializeRequest,
9+
InitializeRequestParams,
10+
JSONRPCMessage,
11+
JSONRPCRequest,
12+
)
413

514

615
@pytest.mark.anyio
@@ -25,3 +34,25 @@ async def test_jsonrpc_request():
2534
assert request.root.method == "initialize"
2635
assert request.root.params is not None
2736
assert request.root.params["protocolVersion"] == LATEST_PROTOCOL_VERSION
37+
38+
39+
@pytest.mark.anyio
40+
async def test_method_initialization():
41+
"""
42+
Test that the method is automatically set on object creation.
43+
Testing just for InitializeRequest to keep the test simple, but should be set for other types as well.
44+
"""
45+
initialize_request = InitializeRequest(
46+
params=InitializeRequestParams(
47+
protocolVersion=LATEST_PROTOCOL_VERSION,
48+
capabilities=ClientCapabilities(),
49+
clientInfo=Implementation(
50+
name="mcp",
51+
version="0.1.0",
52+
),
53+
)
54+
)
55+
56+
assert initialize_request.method == "initialize", "method should be set to 'initialize'"
57+
assert initialize_request.params is not None
58+
assert initialize_request.params.protocolVersion == LATEST_PROTOCOL_VERSION

0 commit comments

Comments
 (0)