From 8485bf250a54ff0bd6bac74a2f756c95c648f102 Mon Sep 17 00:00:00 2001 From: hugocasa Date: Thu, 3 Dec 2026 10:09:08 +0100 Subject: [PATCH] feat: list a role's scripts and flows as tools on the consent page, restricted badge before the access Co-Authored-By: Claude Opus 5.5 --- backend/tests/mcp_role_access.rs | 17 +++++++ backend/windmill-api/openapi.yaml | 34 +++++++++++++ backend/windmill-api/src/mcp/core.rs | 24 ++++++--- backend/windmill-api/src/mcp/oauth_server.rs | 4 ++ backend/windmill-api/src/mcp/role_access.rs | 51 +++++++++++++++++++ backend/windmill-mcp/src/server/runner.rs | 21 ++++++++ .../lib/components/mcp/McpToolsModal.svelte | 23 +++++++-- .../mcp/McpWorkspaceAccessList.svelte | 12 ++--- .../src/lib/components/mcp/toolsets.svelte.ts | 34 +++++++++---- .../src/lib/components/mcp/workspaceAccess.ts | 3 +- .../oauth/mcp_authorize/+page@(root).svelte | 33 +++++++----- 11 files changed, 210 insertions(+), 46 deletions(-) diff --git a/backend/tests/mcp_role_access.rs b/backend/tests/mcp_role_access.rs index 9154061983..feca55640f 100644 --- a/backend/tests/mcp_role_access.rs +++ b/backend/tests/mcp_role_access.rs @@ -140,6 +140,23 @@ async fn role_setting_caps_every_token(db: Pool) -> anyhow::Result<()> } assert!(tools(port, WORKSPACE, "ROLESEL_TOKEN").await.is_empty()); assert!(tools(port, WORKSPACE, "ROLEREAD_TOKEN").await.is_empty()); + // The consent page's preview of a sign-in bound here lists what the connection lists. + let preview: Value = reqwest::Client::new() + .get(format!( + "http://localhost:{port}/api/mcp/gateway/oauth/server/runnable_tools?workspace_id=test-workspace" + )) + .bearer_auth("SECRET_TOKEN") + .send() + .await? + .json() + .await?; + let names: Vec<&str> = preview + .as_array() + .unwrap() + .iter() + .filter_map(|t| t["name"].as_str()) + .collect(); + assert_eq!(names, ["in_set"], "{preview}"); let call = |url: &'static str, token: &'static str, name: &'static str, args: Value| async move { mcp( port, diff --git a/backend/windmill-api/openapi.yaml b/backend/windmill-api/openapi.yaml index 019a0c2cce..42ebaab5ea 100644 --- a/backend/windmill-api/openapi.yaml +++ b/backend/windmill-api/openapi.yaml @@ -29923,6 +29923,40 @@ paths: items: $ref: "#/components/schemas/McpWorkspaceAccess" + /mcp/gateway/oauth/server/runnable_tools: + get: + summary: list the scripts and flows a role offers as MCP tools in a workspace + description: | + What a sign-in bound to the workspace lists where the caller's role offers scripts and + flows as tools, with their arguments; empty under any other role. + operationId: listMcpRunnableTools + tags: + - mcp_oauth + parameters: + - name: workspace_id + in: query + required: true + schema: + type: string + responses: + "200": + description: the tools + content: + application/json: + schema: + type: array + items: + type: object + properties: + name: + type: string + description: + type: string + parameters: + type: object + description: The JSON Schema of its arguments. + required: [name, description, parameters] + /mcp/gateway/oauth/server/client/{client_id}: get: summary: get an MCP client registered with Windmill's OAuth server diff --git a/backend/windmill-api/src/mcp/core.rs b/backend/windmill-api/src/mcp/core.rs index 44f35af475..cddce0af0e 100644 --- a/backend/windmill-api/src/mcp/core.rs +++ b/backend/windmill-api/src/mcp/core.rs @@ -906,6 +906,21 @@ pub async fn add_www_authenticate_header_gateway( } } +/// The tools server's runner, for the consent page to list what a connection would get. Set +/// once the MCP server is set up; unset in a process that serves no MCP. +pub(crate) static PREVIEW_RUNNER: std::sync::OnceLock> = + std::sync::OnceLock::new(); + +impl From for ToolsetEntry { + fn from(tool: windmill_mcp::server::Tool) -> Self { + ToolsetEntry { + name: tool.name.to_string(), + description: tool.description.map(|d| d.to_string()).unwrap_or_default(), + parameters: serde_json::Value::Object((*tool.input_schema).clone()), + } + } +} + /// Setup the MCP server with HTTP transport pub async fn setup_mcp_server( db: DB, @@ -919,6 +934,7 @@ pub async fn setup_mcp_server( let backend = WindmillBackend::new(db.clone(), user_db, base_internal_url, auth_cache); let session_runner = SessionRunner::new(backend.clone()); let runner = Runner::new(backend); + let _ = PREVIEW_RUNNER.set(runner.clone()); let service_config = StreamableHttpServerConfig::default() .with_sse_keep_alive(Some(Duration::from_secs(15))) @@ -1026,7 +1042,7 @@ struct ToolsetQuery { } #[derive(serde::Serialize)] -struct ToolsetEntry { +pub(crate) struct ToolsetEntry { name: String, description: String, /// The JSON Schema of its arguments. @@ -1046,11 +1062,7 @@ async fn list_toolset_handler( Ok(Json( session::session_tools(access) .into_iter() - .map(|tool| ToolsetEntry { - name: tool.name.to_string(), - description: tool.description.map(|d| d.to_string()).unwrap_or_default(), - parameters: serde_json::Value::Object((*tool.input_schema).clone()), - }) + .map(ToolsetEntry::from) .collect(), )) } diff --git a/backend/windmill-api/src/mcp/oauth_server.rs b/backend/windmill-api/src/mcp/oauth_server.rs index 1d4e5c2fdd..daea8b47b3 100644 --- a/backend/windmill-api/src/mcp/oauth_server.rs +++ b/backend/windmill-api/src/mcp/oauth_server.rs @@ -1301,6 +1301,10 @@ pub fn gateway_authed_service() -> Router { "/workspace_access", get(super::role_access::list_workspace_access), ) + .route( + "/runnable_tools", + get(super::role_access::list_runnable_tools), + ) } #[cfg(test)] diff --git a/backend/windmill-api/src/mcp/role_access.rs b/backend/windmill-api/src/mcp/role_access.rs index b740261a86..efd794a737 100644 --- a/backend/windmill-api/src/mcp/role_access.rs +++ b/backend/windmill-api/src/mcp/role_access.rs @@ -320,6 +320,57 @@ pub async fn list_workspace_access( Ok(Json(out)) } +#[derive(Deserialize)] +pub struct RunnableToolsQuery { + workspace_id: String, +} + +/// GET /api/mcp/gateway/oauth/server/runnable_tools: the scripts and flows a sign-in bound to a +/// workspace would list where the caller's role offers them as tools, with their arguments. +pub async fn list_runnable_tools( + Extension(db): Extension, + authed: ApiAuthed, + Query(query): Query, +) -> JsonResult> { + if authed + .scopes + .as_deref() + .is_some_and(|s| s.iter().any(|x| !x.starts_with("if_jobs:filter_tags:"))) + { + return Err(Error::NotAuthorized( + "A scope-restricted token cannot list its owner's tools".to_string(), + )); + } + let w_id = query.workspace_id; + let username = accesses( + &db, + &authed.email, + vec![w_id.clone()], + SessionAccess::Full, + false, + ) + .await? + .remove(&w_id) + .and_then(|m| m.username) + .ok_or_else(|| Error::NotFound(format!("You are not a member of '{w_id}'")))?; + let runner = super::core::PREVIEW_RUNNER + .get() + .ok_or_else(|| Error::BadRequest("This server does not serve MCP".to_string()))?; + let there: ApiAuthed = windmill_common::auth::fetch_authed_from_permissioned_as( + &format!("u/{username}"), + &authed.email, + &w_id, + &db, + ) + .await? + .into(); + let tools = runner + .role_runnable_tools(&there, &w_id) + .await + .map_err(|e| Error::internal_err(e.message.into_owned()))?; + Ok(Json(tools.tools.into_iter().map(Into::into).collect())) +} + async fn count_runnables( db: &DB, user_db: &UserDB, diff --git a/backend/windmill-mcp/src/server/runner.rs b/backend/windmill-mcp/src/server/runner.rs index aebf89219c..a6a15ddab6 100644 --- a/backend/windmill-mcp/src/server/runner.rs +++ b/backend/windmill-mcp/src/server/runner.rs @@ -819,6 +819,27 @@ impl Runner { .collect()) } + /// What a sign-in bound to `workspace_id` lists where the caller's role offers scripts and + /// flows as tools, for the consent page to show; empty under any other role. `auth` is the + /// caller as a member of that workspace. + pub async fn role_runnable_tools( + &self, + auth: &B::Auth, + workspace_id: &str, + ) -> Result { + let Some(limit @ McpLimit::Runnables { .. }) = + self.backend.mcp_limit(auth, workspace_id).await? + else { + return Ok(ListToolsResult::default()); + }; + let scope_config = parse_mcp_scopes(&["mcp:all".to_string()]) + .map_err(|e| ErrorData::internal_error(e, None))? + .limited_by(&limit); + let read_only = auth.read_only() || scope_config.read_run || scope_config.read_only; + self.list_tools_single(auth, workspace_id, &scope_config, read_only) + .await + } + /// List tools for a single, bound workspace (URL-path or token-bound). async fn list_tools_single( &self, diff --git a/frontend/src/lib/components/mcp/McpToolsModal.svelte b/frontend/src/lib/components/mcp/McpToolsModal.svelte index e10a77a23b..c25a14f4d3 100644 --- a/frontend/src/lib/components/mcp/McpToolsModal.svelte +++ b/frontend/src/lib/components/mcp/McpToolsModal.svelte @@ -1,12 +1,23 @@