context_server: Make notifications type safe (#32396)

Follow up to #32254 

Release Notes:

- N/A
This commit is contained in:
Bennet Bo Fenner 2025-06-09 17:11:01 +02:00 committed by GitHub
parent 3853e83da7
commit 6801b9137f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 67 additions and 43 deletions

View file

@ -8,7 +8,7 @@
use anyhow::Result;
use crate::client::Client;
use crate::types::{self, Request};
use crate::types::{self, Notification, Request};
pub struct ModelContextProtocol {
inner: Client,
@ -43,7 +43,7 @@ impl ModelContextProtocol {
let response: types::InitializeResponse = self
.inner
.request(types::request::Initialize::METHOD, params)
.request(types::requests::Initialize::METHOD, params)
.await?;
anyhow::ensure!(
@ -54,16 +54,13 @@ impl ModelContextProtocol {
log::trace!("mcp server info {:?}", response.server_info);
self.inner.notify(
types::NotificationType::Initialized.as_str(),
serde_json::json!({}),
)?;
let initialized_protocol = InitializedContextServerProtocol {
inner: self.inner,
initialize: response,
};
initialized_protocol.notify::<types::notifications::Initialized>(())?;
Ok(initialized_protocol)
}
}
@ -97,4 +94,8 @@ impl InitializedContextServerProtocol {
pub async fn request<T: Request>(&self, params: T::Params) -> Result<T::Response> {
self.inner.request(T::METHOD, params).await
}
pub fn notify<T: Notification>(&self, params: T::Params) -> Result<()> {
self.inner.notify(T::METHOD, params)
}
}