feat(#1102): add list_invites and join_room to matrix MCP

This commit is contained in:
damocles 2026-06-03 01:10:42 +02:00
commit 7e5a21522a
4 changed files with 85 additions and 3 deletions

View file

@ -13,7 +13,7 @@ use matrix_sdk::{
Client,
room::reply::{EnforceThread, Reply},
ruma::{
OwnedEventId, OwnedRoomId, OwnedUserId, RoomOrAliasId,
OwnedEventId, OwnedRoomId, OwnedServerName, OwnedUserId, RoomOrAliasId,
api::client::receipt::create_receipt::v3::ReceiptType,
events::{
reaction::ReactionEventContent,
@ -27,6 +27,14 @@ use serde::Serialize;
use crate::protocol::DaemonResponse;
/// JSON-shape for `list_invites`: one row per pending room invite.
#[derive(Debug, Serialize)]
pub struct InviteInfo {
pub room_id: String,
pub canonical_alias: Option<String>,
pub name: Option<String>,
}
/// JSON-shape for `list_rooms`: one row per joined room.
#[derive(Debug, Serialize)]
pub struct RoomInfo {
@ -267,6 +275,36 @@ pub async fn list_room_members(client: &Client, room_ref: &str) -> DaemonRespons
DaemonResponse::ok(&list)
}
pub async fn list_invites(client: &Client) -> DaemonResponse {
let invites: Vec<InviteInfo> = client
.invited_rooms()
.into_iter()
.map(|room| InviteInfo {
room_id: room.room_id().to_string(),
canonical_alias: room.canonical_alias().map(|a| a.to_string()),
name: room.name(),
})
.collect();
DaemonResponse::ok(&invites)
}
pub async fn join_room(client: &Client, room_ref: &str) -> DaemonResponse {
let parsed: &RoomOrAliasId = match room_ref.try_into() {
Ok(p) => p,
Err(e) => {
return DaemonResponse::error(format!("invalid room reference {room_ref}: {e}"));
}
};
let server_names: Vec<OwnedServerName> = vec![];
match client.join_room_by_id_or_alias(parsed, &server_names).await {
Ok(room) => DaemonResponse::ok(&serde_json::json!({
"joined": true,
"room_id": room.room_id().to_string(),
})),
Err(e) => DaemonResponse::error(format!("join room {room_ref}: {e}")),
}
}
pub async fn read_room(client: &Client, room_ref: &str, limit: Option<usize>) -> DaemonResponse {
use matrix_sdk::ruma::api::Direction;
use matrix_sdk::ruma::api::client::message::get_message_events;