Merge pull request #45 from modelcontextprotocol/davidsp/client-capabilities
feat: add client capability checking to ServerSession
This commit is contained in:
commit
bb3894a630
1 changed files with 38 additions and 1 deletions
|
|
@ -30,6 +30,7 @@ class ServerSession(
|
||||||
]
|
]
|
||||||
):
|
):
|
||||||
_initialized: InitializationState = InitializationState.NotInitialized
|
_initialized: InitializationState = InitializationState.NotInitialized
|
||||||
|
_client_params: types.InitializeRequestParams | None = None
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
|
|
@ -43,12 +44,47 @@ class ServerSession(
|
||||||
self._initialization_state = InitializationState.NotInitialized
|
self._initialization_state = InitializationState.NotInitialized
|
||||||
self._init_options = init_options
|
self._init_options = init_options
|
||||||
|
|
||||||
|
@property
|
||||||
|
def client_params(self) -> types.InitializeRequestParams | None:
|
||||||
|
return self._client_params
|
||||||
|
|
||||||
|
def check_client_capability(self, capability: types.ClientCapabilities) -> bool:
|
||||||
|
"""Check if the client supports a specific capability."""
|
||||||
|
if self._client_params is None:
|
||||||
|
return False
|
||||||
|
|
||||||
|
# Get client capabilities from initialization params
|
||||||
|
client_caps = self._client_params.capabilities
|
||||||
|
|
||||||
|
# Check each specified capability in the passed in capability object
|
||||||
|
if capability.roots is not None:
|
||||||
|
if client_caps.roots is None:
|
||||||
|
return False
|
||||||
|
if capability.roots.listChanged and not client_caps.roots.listChanged:
|
||||||
|
return False
|
||||||
|
|
||||||
|
if capability.sampling is not None:
|
||||||
|
if client_caps.sampling is None:
|
||||||
|
return False
|
||||||
|
|
||||||
|
if capability.experimental is not None:
|
||||||
|
if client_caps.experimental is None:
|
||||||
|
return False
|
||||||
|
# Check each experimental capability
|
||||||
|
for exp_key, exp_value in capability.experimental.items():
|
||||||
|
if (exp_key not in client_caps.experimental or
|
||||||
|
client_caps.experimental[exp_key] != exp_value):
|
||||||
|
return False
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
async def _received_request(
|
async def _received_request(
|
||||||
self, responder: RequestResponder[types.ClientRequest, types.ServerResult]
|
self, responder: RequestResponder[types.ClientRequest, types.ServerResult]
|
||||||
):
|
):
|
||||||
match responder.request.root:
|
match responder.request.root:
|
||||||
case types.InitializeRequest():
|
case types.InitializeRequest(params=params):
|
||||||
self._initialization_state = InitializationState.Initializing
|
self._initialization_state = InitializationState.Initializing
|
||||||
|
self._client_params = params
|
||||||
await responder.respond(
|
await responder.respond(
|
||||||
types.ServerResult(
|
types.ServerResult(
|
||||||
types.InitializeResult(
|
types.InitializeResult(
|
||||||
|
|
@ -81,6 +117,7 @@ class ServerSession(
|
||||||
"Received notification before initialization was complete"
|
"Received notification before initialization was complete"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
async def send_log_message(
|
async def send_log_message(
|
||||||
self, level: types.LoggingLevel, data: Any, logger: str | None = None
|
self, level: types.LoggingLevel, data: Any, logger: str | None = None
|
||||||
) -> None:
|
) -> None:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue