Initialize the MCP connection
(&self)
| 53 | |
| 54 | /// Initialize the MCP connection |
| 55 | pub async fn initialize(&self) -> Result<InitializeResult> { |
| 56 | let params = InitializeParams { |
| 57 | protocol_version: PROTOCOL_VERSION.to_string(), |
| 58 | capabilities: ClientCapabilities::default(), |
| 59 | client_info: ClientInfo { |
| 60 | name: "a3s-code".to_string(), |
| 61 | version: env!("CARGO_PKG_VERSION").to_string(), |
| 62 | }, |
| 63 | }; |
| 64 | |
| 65 | let request = JsonRpcRequest::new( |
| 66 | self.next_id(), |
| 67 | "initialize", |
| 68 | Some(serde_json::to_value(¶ms)?), |
| 69 | ); |
| 70 | |
| 71 | let response = self.transport.request(request).await?; |
| 72 | |
| 73 | if let Some(error) = response.error { |
| 74 | return Err(anyhow!( |
| 75 | "MCP initialize error: {} ({})", |
| 76 | error.message, |
| 77 | error.code |
| 78 | )); |
| 79 | } |
| 80 | |
| 81 | let result: InitializeResult = serde_json::from_value( |
| 82 | response |
| 83 | .result |
| 84 | .ok_or_else(|| anyhow!("No result in response"))?, |
| 85 | )?; |
| 86 | |
| 87 | // Store capabilities |
| 88 | { |
| 89 | let mut caps = self.capabilities.write().await; |
| 90 | *caps = result.capabilities.clone(); |
| 91 | } |
| 92 | |
| 93 | // Send initialized notification |
| 94 | let notification = JsonRpcNotification::new("notifications/initialized", None); |
| 95 | self.transport.notify(notification).await?; |
| 96 | |
| 97 | // Mark as initialized |
| 98 | { |
| 99 | let mut init = self.initialized.write().await; |
| 100 | *init = true; |
| 101 | } |
| 102 | |
| 103 | tracing::info!( |
| 104 | "MCP client '{}' initialized with server '{}' v{}", |
| 105 | self.name, |
| 106 | result.server_info.name, |
| 107 | result.server_info.version |
| 108 | ); |
| 109 | |
| 110 | Ok(result) |
| 111 | } |
| 112 |