Skip to content

Commit b4cfb44

Browse files
committed
Add MCP server for AI agent integration with Buck2
Implements a Model Context Protocol (MCP) server that allows AI agents to interact with Buck2 through a JSON-RPC interface over stdio. Features: - MCP protocol implementation with JSON-RPC 2.0 transport - Daemon client for connecting to running Buck2 daemon - Tools: buck2_build, buck2_query (cquery/uquery), buck2_targets - Async build support with operation polling for long-running builds - Operation manager for tracking and cancelling async operations Signed-off-by: Daniel Hodges <hodgesd@meta.com>
1 parent e875b7a commit b4cfb44

File tree

22 files changed

+2604
-0
lines changed

22 files changed

+2604
-0
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ members = [
4848
"app/buck2_grpc",
4949
"app/buck2_health_check",
5050
"app/buck2_health_check_proto",
51+
"app/buck2_mcp",
5152
"app/buck2_http",
5253
"app/buck2_install_proto",
5354
"app/buck2_interpreter",
@@ -434,6 +435,7 @@ buck2_host_sharing_proto = { path = "app/buck2_host_sharing_proto" }
434435
buck2_http = { path = "app/buck2_http" }
435436
buck2_install_proto = { path = "app/buck2_install_proto" }
436437
buck2_interpreter = { path = "app/buck2_interpreter" }
438+
buck2_mcp = { path = "app/buck2_mcp" }
437439
buck2_interpreter_for_build = { path = "app/buck2_interpreter_for_build" }
438440
buck2_interpreter_for_build_tests = { path = "app/buck2_interpreter_for_build_tests" }
439441
buck2_miniperf = { path = "app/buck2_miniperf" }

app/buck2_mcp/BUCK

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
load("@fbcode//buck2:buck_rust_binary.bzl", "buck_rust_binary")
2+
load("@fbsource//tools/build_defs:rust_library.bzl", "rust_library")
3+
4+
oncall("build_infra")
5+
6+
rust_library(
7+
name = "buck2_mcp",
8+
srcs = glob([
9+
"src/**/*.rs",
10+
]),
11+
test_deps = [
12+
"fbsource//third-party/rust:tempfile",
13+
],
14+
deps = [
15+
"fbsource//third-party/rust:async-trait",
16+
"fbsource//third-party/rust:chrono",
17+
"fbsource//third-party/rust:dirs",
18+
"fbsource//third-party/rust:futures",
19+
"fbsource//third-party/rust:prost-0-13-4",
20+
"fbsource//third-party/rust:serde",
21+
"fbsource//third-party/rust:serde_json",
22+
"fbsource//third-party/rust:tokio",
23+
"fbsource//third-party/rust:tonic-0-12-3",
24+
"fbsource//third-party/rust:tracing",
25+
"fbsource//third-party/rust:tracing-subscriber",
26+
"fbsource//third-party/rust:uuid",
27+
"//buck2/app/buck2_cli_proto:buck2_cli_proto",
28+
"//buck2/app/buck2_common:buck2_common",
29+
"//buck2/app/buck2_core:buck2_core",
30+
"//buck2/app/buck2_error:buck2_error",
31+
"//buck2/app/buck2_fs:buck2_fs",
32+
"//buck2/app/buck2_util:buck2_util",
33+
],
34+
)
35+
36+
buck_rust_binary(
37+
name = "buck2-mcp",
38+
srcs = ["src/bin/buck2-mcp.rs"],
39+
crate = "buck2_mcp_bin",
40+
crate_root = "src/bin/buck2-mcp.rs",
41+
deps = [
42+
":buck2_mcp",
43+
"fbsource//third-party/rust:tokio",
44+
"fbsource//third-party/rust:tracing",
45+
"fbsource//third-party/rust:tracing-subscriber",
46+
"//buck2/app/buck2_error:buck2_error",
47+
"//buck2/app/buck2_fs:buck2_fs",
48+
],
49+
)

app/buck2_mcp/Cargo.toml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
[package]
2+
name = "buck2_mcp"
3+
description = "Buck2 MCP (Model Context Protocol) server for AI agent integration"
4+
edition = "2024"
5+
license = { workspace = true }
6+
repository = { workspace = true }
7+
version = "0.1.0"
8+
9+
[[bin]]
10+
name = "buck2-mcp"
11+
path = "src/bin/buck2-mcp.rs"
12+
13+
[dependencies]
14+
async-trait = { workspace = true }
15+
chrono = { workspace = true }
16+
dirs = { workspace = true }
17+
futures = { workspace = true }
18+
prost = { workspace = true }
19+
serde = { workspace = true }
20+
serde_json = { workspace = true }
21+
tokio = { workspace = true }
22+
tonic = { workspace = true }
23+
tracing = { workspace = true }
24+
tracing-subscriber = { workspace = true }
25+
uuid = { workspace = true }
26+
27+
buck2_cli_proto = { workspace = true }
28+
buck2_common = { workspace = true }
29+
buck2_core = { workspace = true }
30+
buck2_error = { workspace = true }
31+
buck2_fs = { workspace = true }
32+
buck2_util = { workspace = true }
33+
34+
[dev-dependencies]
35+
tempfile = { workspace = true }

app/buck2_mcp/src/bin/buck2-mcp.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is dual-licensed under either the MIT license found in the
5+
* LICENSE-MIT file in the root directory of this source tree or the Apache
6+
* License, Version 2.0 found in the LICENSE-APACHE file in the root directory
7+
* of this source tree. You may select, at your option, one of the
8+
* above-listed licenses.
9+
*/
10+
11+
//! Buck2 MCP server binary entry point.
12+
13+
use std::env;
14+
15+
use tracing::info;
16+
use tracing_subscriber::EnvFilter;
17+
18+
#[tokio::main]
19+
async fn main() -> buck2_error::Result<()> {
20+
// Initialize tracing to stderr (stdout is used for MCP protocol)
21+
tracing_subscriber::fmt()
22+
.with_env_filter(EnvFilter::from_default_env())
23+
.with_writer(std::io::stderr)
24+
.init();
25+
26+
info!("Starting Buck2 MCP server");
27+
28+
let working_dir = env::current_dir()
29+
.map_err(|e| {
30+
buck2_error::buck2_error!(
31+
buck2_error::ErrorTag::Environment,
32+
"Failed to get current directory: {}",
33+
e
34+
)
35+
})?
36+
.to_string_lossy()
37+
.to_string();
38+
39+
info!("Working directory: {}", working_dir);
40+
41+
let mut server = buck2_mcp::mcp::server::McpServer::new(working_dir);
42+
server.run().await
43+
}

app/buck2_mcp/src/daemon.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is dual-licensed under either the MIT license found in the
5+
* LICENSE-MIT file in the root directory of this source tree or the Apache
6+
* License, Version 2.0 found in the LICENSE-APACHE file in the root directory
7+
* of this source tree. You may select, at your option, one of the
8+
* above-listed licenses.
9+
*/
10+
11+
//! Buck2 daemon client wrapper.
12+
13+
pub mod client;

0 commit comments

Comments
 (0)