|
6 | 6 | import logging |
7 | 7 | import uuid |
8 | 8 | from collections.abc import Callable |
9 | | -from dataclasses import dataclass, field |
10 | 9 | from typing import Any, TypeVar |
11 | 10 |
|
12 | 11 | import msgspec |
|
37 | 36 | PAGE_SIZE = 50 |
38 | 37 |
|
39 | 38 |
|
40 | | -@dataclass |
41 | 39 | class MCPServer: |
42 | 40 | """Base MCP Server implementation""" |
43 | 41 |
|
44 | | - name: str |
45 | | - version: str |
46 | | - |
47 | | - # The response queue is used to store responses |
48 | | - # that are sent to the client via SSE. |
49 | | - response_queue: ResponseQueueProtocol |
50 | | - |
51 | | - # When the client send an `initialize` request, it |
52 | | - # sends the highest version it can support. The server |
53 | | - # is expected to respond with the latest version supported |
54 | | - # by the server. |
55 | | - # The client is responsible to disconnect if the version is |
56 | | - # not supported. |
57 | | - # https://spec.modelcontextprotocol.io/specification/2025-06-18/basic/lifecycle/#version-negotiation |
58 | | - protocol_version: str = "2025-06-18" |
59 | | - |
60 | | - # server capabilities can be: |
61 | | - # - prompts |
62 | | - # - resources |
63 | | - # - tools |
64 | | - # - logging |
65 | | - # - experimental |
66 | | - _prompts: dict[str, Callable] = field(default_factory=dict) |
67 | | - _prompts_list: dict[str, PromptInfo] = field(default_factory=dict) |
68 | | - _prompt_completions: dict[str, dict[str, Callable]] = field(default_factory=dict) |
69 | | - |
70 | | - _resources: dict[str, Callable] = field(default_factory=dict) |
71 | | - _resources_list: dict[str, ResourceInfo] = field(default_factory=dict) |
72 | | - |
73 | | - _resource_templates: dict[str, Callable] = field(default_factory=dict) |
74 | | - _resource_template_list: dict[str, ResourceInfo] = field(default_factory=dict) |
75 | | - |
76 | | - _tools: dict[str, Callable] = field(default_factory=dict) |
77 | | - _tools_list: dict[str, ToolInfo] = field(default_factory=dict) |
78 | | - _tool_arg_models: dict[str, type] = field(default_factory=dict) |
| 42 | + def __init__( |
| 43 | + self, |
| 44 | + name: str, |
| 45 | + version: str, |
| 46 | + response_queue: ResponseQueueProtocol, |
| 47 | + instructions: str = "", |
| 48 | + protocol_version: str = "2025-06-18", |
| 49 | + ) -> None: |
| 50 | + # Basic info |
| 51 | + self.name: str = name |
| 52 | + self.version: str = version |
| 53 | + self.instructions: str = instructions |
| 54 | + |
| 55 | + # The response queue is used to store responses |
| 56 | + # that are sent to the client via SSE. |
| 57 | + self.response_queue: ResponseQueueProtocol = response_queue |
| 58 | + |
| 59 | + # Protocol version negotiation (see MCP spec) |
| 60 | + self.protocol_version: str = protocol_version |
| 61 | + |
| 62 | + # Server capabilities registries |
| 63 | + self._prompts: dict[str, Callable] = {} |
| 64 | + self._prompts_list: dict[str, PromptInfo] = {} |
| 65 | + self._prompt_completions: dict[str, dict[str, Callable]] = {} |
| 66 | + |
| 67 | + self._resources: dict[str, Callable] = {} |
| 68 | + self._resources_list: dict[str, ResourceInfo] = {} |
| 69 | + |
| 70 | + self._resource_templates: dict[str, Callable] = {} |
| 71 | + self._resource_template_list: dict[str, ResourceInfo] = {} |
| 72 | + |
| 73 | + self._tools: dict[str, Callable] = {} |
| 74 | + self._tools_list: dict[str, ToolInfo] = {} |
| 75 | + self._tool_arg_models: dict[str, type] = {} |
79 | 76 |
|
80 | 77 | def register_tool(self, name: str, callable: Callable, tool_info: ToolInfo) -> None: |
81 | 78 | self._tools_list[name] = tool_info |
@@ -248,6 +245,7 @@ def get_capabilities(self) -> InitializeResult: |
248 | 245 | resources={"subscribe": False, "listChanged": False}, |
249 | 246 | tools={"listChanged": False}, |
250 | 247 | ), |
| 248 | + instructions=self.instructions, |
251 | 249 | protocolVersion=self.protocol_version, |
252 | 250 | serverInfo=ServerInfo(name=self.name, version=self.version), |
253 | 251 | ) |
|
0 commit comments