mirror of
https://github.com/aljazceru/mcp-python-sdk.git
synced 2025-12-19 14:54:24 +01:00
Allow passing initialization options to a session
We need a way for servers to pass initialization options to the session. This is the beginning of this.
This commit is contained in:
@@ -32,6 +32,7 @@ from mcp_python.types import (
|
||||
ReadResourceResult,
|
||||
Resource,
|
||||
ResourceReference,
|
||||
ServerCapabilities,
|
||||
ServerResult,
|
||||
SetLevelRequest,
|
||||
SubscribeRequest,
|
||||
@@ -40,7 +41,6 @@ from mcp_python.types import (
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
request_ctx: contextvars.ContextVar[RequestContext] = contextvars.ContextVar(
|
||||
"request_ctx"
|
||||
)
|
||||
@@ -53,6 +53,33 @@ class Server:
|
||||
self.notification_handlers: dict[type, Callable[..., Awaitable[None]]] = {}
|
||||
logger.info(f"Initializing server '{name}'")
|
||||
|
||||
def create_initialization_options(self) -> types.InitializationOptions:
|
||||
"""Create initialization options from this server instance."""
|
||||
def pkg_version(package: str) -> str:
|
||||
try:
|
||||
from importlib.metadata import version
|
||||
return version(package)
|
||||
except Exception:
|
||||
return "unknown"
|
||||
|
||||
return types.InitializationOptions(
|
||||
server_name=self.name,
|
||||
server_version=pkg_version("mcp_python"),
|
||||
capabilities=self.get_capabilities(),
|
||||
)
|
||||
|
||||
def get_capabilities(self) -> ServerCapabilities:
|
||||
"""Convert existing handlers to a ServerCapabilities object."""
|
||||
def get_capability(req_type: type) -> dict[str, Any] | None:
|
||||
return {} if req_type in self.request_handlers else None
|
||||
|
||||
return ServerCapabilities(
|
||||
prompts=get_capability(ListPromptsRequest),
|
||||
resources=get_capability(ListResourcesRequest),
|
||||
tools=get_capability(ListPromptsRequest),
|
||||
logging=get_capability(SetLevelRequest)
|
||||
)
|
||||
|
||||
@property
|
||||
def request_context(self) -> RequestContext:
|
||||
"""If called outside of a request context, this will raise a LookupError."""
|
||||
@@ -280,9 +307,10 @@ class Server:
|
||||
self,
|
||||
read_stream: MemoryObjectReceiveStream[JSONRPCMessage | Exception],
|
||||
write_stream: MemoryObjectSendStream[JSONRPCMessage],
|
||||
initialization_options: types.InitializationOptions
|
||||
):
|
||||
with warnings.catch_warnings(record=True) as w:
|
||||
async with ServerSession(read_stream, write_stream) as session:
|
||||
async with ServerSession(read_stream, write_stream, initialization_options) as session:
|
||||
async for message in session.incoming_messages:
|
||||
logger.debug(f"Received message: {message}")
|
||||
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
import logging
|
||||
import sys
|
||||
|
||||
import importlib.metadata
|
||||
import anyio
|
||||
|
||||
from mcp_python.server.session import ServerSession
|
||||
from mcp_python.server.types import InitializationOptions
|
||||
from mcp_python.server.stdio import stdio_server
|
||||
from mcp_python.types import ServerCapabilities
|
||||
|
||||
if not sys.warnoptions:
|
||||
import warnings
|
||||
@@ -26,8 +28,9 @@ async def receive_loop(session: ServerSession):
|
||||
|
||||
|
||||
async def main():
|
||||
version = importlib.metadata.version("mcp_python")
|
||||
async with stdio_server() as (read_stream, write_stream):
|
||||
async with ServerSession(read_stream, write_stream) as session, write_stream:
|
||||
async with ServerSession(read_stream, write_stream, InitializationOptions(server_name="mcp_python", server_version=version, capabilities=ServerCapabilities())) as session, write_stream:
|
||||
await receive_loop(session)
|
||||
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ from mcp_python.shared.session import (
|
||||
BaseSession,
|
||||
RequestResponder,
|
||||
)
|
||||
from mcp_python.server.types import InitializationOptions
|
||||
from mcp_python.shared.version import SUPPORTED_PROTOCOL_VERSION
|
||||
from mcp_python.types import (
|
||||
ClientNotification,
|
||||
@@ -52,9 +53,11 @@ class ServerSession(
|
||||
self,
|
||||
read_stream: MemoryObjectReceiveStream[JSONRPCMessage | Exception],
|
||||
write_stream: MemoryObjectSendStream[JSONRPCMessage],
|
||||
init_options: InitializationOptions
|
||||
) -> None:
|
||||
super().__init__(read_stream, write_stream, ClientRequest, ClientNotification)
|
||||
self._initialization_state = InitializationState.NotInitialized
|
||||
self._init_options = init_options
|
||||
|
||||
async def _received_request(
|
||||
self, responder: RequestResponder[ClientRequest, ServerResult]
|
||||
@@ -66,15 +69,10 @@ class ServerSession(
|
||||
ServerResult(
|
||||
InitializeResult(
|
||||
protocolVersion=SUPPORTED_PROTOCOL_VERSION,
|
||||
capabilities=ServerCapabilities(
|
||||
logging=None,
|
||||
resources=None,
|
||||
tools=None,
|
||||
experimental=None,
|
||||
prompts={},
|
||||
),
|
||||
capabilities=self._init_options.capabilities,
|
||||
serverInfo=Implementation(
|
||||
name="mcp_python", version="0.1.0"
|
||||
name=self._init_options.server_name,
|
||||
version=self._init_options.server_version
|
||||
),
|
||||
)
|
||||
)
|
||||
|
||||
@@ -5,7 +5,8 @@ This module provides simpler types to use with the server for managing prompts.
|
||||
from dataclasses import dataclass
|
||||
from typing import Literal
|
||||
|
||||
from mcp_python.types import Role
|
||||
from pydantic import BaseModel
|
||||
from mcp_python.types import Role, ServerCapabilities
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -25,3 +26,9 @@ class Message:
|
||||
class PromptResponse:
|
||||
messages: list[Message]
|
||||
desc: str | None = None
|
||||
|
||||
|
||||
class InitializationOptions(BaseModel):
|
||||
server_name: str
|
||||
server_version: str
|
||||
capabilities: ServerCapabilities
|
||||
|
||||
Reference in New Issue
Block a user