Skip to content

Initial MCP implementation #81770

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Jul 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 112 additions & 4 deletions crates/napi/src/next_api/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,25 @@ use anyhow::Result;
use futures_util::TryFutureExt;
use napi::{JsFunction, bindgen_prelude::External};
use next_api::{
module_graph_snapshot::{ModuleGraphSnapshot, get_module_graph_snapshot},
operation::OptionEndpoint,
paths::ServerPath,
route::{
EndpointOutputPaths, endpoint_client_changed_operation, endpoint_server_changed_operation,
endpoint_write_to_disk_operation,
Endpoint, EndpointOutputPaths, endpoint_client_changed_operation,
endpoint_server_changed_operation, endpoint_write_to_disk_operation,
},
};
use tracing::Instrument;
use turbo_tasks::{Completion, Effects, OperationVc, ReadRef, Vc};
use turbopack_core::{diagnostics::PlainDiagnostic, issue::PlainIssue};
use turbo_tasks::{
Completion, Effects, OperationVc, ReadRef, TryFlatJoinIterExt, TryJoinIterExt, Vc,
};
use turbopack_core::{diagnostics::PlainDiagnostic, error::PrettyPrintError, issue::PlainIssue};

use super::utils::{
DetachedVc, NapiDiagnostic, NapiIssue, RootTask, TurbopackResult,
strongly_consistent_catch_collectables, subscribe,
};
use crate::next_api::module_graph::NapiModuleGraphSnapshot;

#[napi(object)]
#[derive(Default)]
Expand Down Expand Up @@ -81,6 +85,11 @@ impl From<Option<EndpointOutputPaths>> for NapiWrittenEndpoint {
}
}

#[napi(object)]
pub struct NapiModuleGraphSnapshots {
pub module_graphs: Vec<NapiModuleGraphSnapshot>,
}

// NOTE(alexkirsz) We go through an extra layer of indirection here because of
// two factors:
// 1. rustc currently has a bug where using a dyn trait as a type argument to
Expand Down Expand Up @@ -155,6 +164,105 @@ pub async fn endpoint_write_to_disk(
})
}

#[turbo_tasks::value(serialization = "none")]
struct ModuleGraphsWithIssues {
module_graphs: Option<ReadRef<ModuleGraphSnapshots>>,
issues: Arc<Vec<ReadRef<PlainIssue>>>,
diagnostics: Arc<Vec<ReadRef<PlainDiagnostic>>>,
effects: Arc<Effects>,
}

#[turbo_tasks::function(operation)]
async fn get_module_graphs_with_issues_operation(
endpoint_op: OperationVc<OptionEndpoint>,
) -> Result<Vc<ModuleGraphsWithIssues>> {
let module_graphs_op = get_module_graphs_operation(endpoint_op);
let (module_graphs, issues, diagnostics, effects) =
strongly_consistent_catch_collectables(module_graphs_op).await?;
Ok(ModuleGraphsWithIssues {
module_graphs,
issues,
diagnostics,
effects,
}
.cell())
}

#[turbo_tasks::value(transparent)]
struct ModuleGraphSnapshots(Vec<ReadRef<ModuleGraphSnapshot>>);

#[turbo_tasks::function(operation)]
async fn get_module_graphs_operation(
endpoint_op: OperationVc<OptionEndpoint>,
) -> Result<Vc<ModuleGraphSnapshots>> {
let Some(endpoint) = *endpoint_op.connect().await? else {
return Ok(Vc::cell(vec![]));
};
let graphs = endpoint.module_graphs().await?;
let entries = endpoint.entries().await?;
let entry_modules = entries.iter().flat_map(|e| e.entries()).collect::<Vec<_>>();
let snapshots = graphs
.iter()
.map(async |&graph| {
let module_graph = graph.await?;
let entry_modules = entry_modules
.iter()
.map(async |&m| Ok(module_graph.has_entry(m).await?.then_some(m)))
.try_flat_join()
.await?;
Ok((*graph, entry_modules))
})
.try_join()
.await?
.into_iter()
.map(|(graph, entry_modules)| (graph, Vc::cell(entry_modules)))
.collect::<Vec<_>>()
.into_iter()
.map(async |(graph, entry_modules)| {
get_module_graph_snapshot(graph, Some(entry_modules)).await
})
.try_join()
.await?;
Ok(Vc::cell(snapshots))
}

#[napi]
pub async fn endpoint_module_graphs(
#[napi(ts_arg_type = "{ __napiType: \"Endpoint\" }")] endpoint: External<ExternalEndpoint>,
) -> napi::Result<TurbopackResult<NapiModuleGraphSnapshots>> {
let endpoint_op: OperationVc<OptionEndpoint> = ***endpoint;
let (module_graphs, issues, diagnostics) = endpoint
.turbopack_ctx()
.turbo_tasks()
.run_once(async move {
let module_graphs_op = get_module_graphs_with_issues_operation(endpoint_op);
let ModuleGraphsWithIssues {
module_graphs,
issues,
diagnostics,
effects: _,
} = &*module_graphs_op.connect().await?;
Ok((module_graphs.clone(), issues.clone(), diagnostics.clone()))
})
.await
.map_err(|e| napi::Error::from_reason(PrettyPrintError(&e).to_string()))?;

Ok(TurbopackResult {
result: NapiModuleGraphSnapshots {
module_graphs: module_graphs
.into_iter()
.flat_map(|m| m.into_iter())
.map(|m| NapiModuleGraphSnapshot::from(&**m))
.collect(),
},
issues: issues.iter().map(|i| NapiIssue::from(&**i)).collect(),
diagnostics: diagnostics
.iter()
.map(|d| NapiDiagnostic::from(d))
.collect(),
})
}

#[napi(ts_return_type = "{ __napiType: \"RootTask\" }")]
pub fn endpoint_server_changed_subscribe(
#[napi(ts_arg_type = "{ __napiType: \"Endpoint\" }")] endpoint: External<ExternalEndpoint>,
Expand Down
1 change: 1 addition & 0 deletions crates/napi/src/next_api/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod endpoint;
pub mod module_graph;
pub mod project;
pub mod turbopack_ctx;
pub mod utils;
98 changes: 98 additions & 0 deletions crates/napi/src/next_api/module_graph.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
use next_api::module_graph_snapshot::{ModuleGraphSnapshot, ModuleInfo, ModuleReference};
use turbo_rcstr::RcStr;
use turbopack_core::chunk::ChunkingType;

#[napi(object)]
pub struct NapiModuleReference {
/// The index of the referenced/referencing module in the modules list.
pub index: u32,
/// The export used in the module reference.
pub export: String,
/// The type of chunking for the module reference.
pub chunking_type: String,
}

impl From<&ModuleReference> for NapiModuleReference {
fn from(reference: &ModuleReference) -> Self {
Self {
index: reference.index as u32,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Silent integer truncation when converting module reference indices from usize to u32.

View Details

Analysis

The code uses an as cast to convert reference.index from usize to u32. This will silently truncate values larger than u32::MAX on 64-bit systems, potentially causing incorrect module references and broken module graph relationships.

The ModuleReference.index field is usize but NapiModuleReference.index is u32. When the index exceeds u32::MAX, the cast will wrap around, leading to incorrect array access patterns in the JavaScript layer.


Recommendation

Replace the as cast with a checked conversion:

index: reference.index.try_into().unwrap_or_else(|_| {
    tracing::warn!("Module reference index {} exceeds u32::MAX, using u32::MAX", reference.index);
    u32::MAX
}),

This ensures the issue is logged when it occurs and provides a safer fallback value.


👍 or 👎 to improve Vade.

export: reference.export.to_string(),
chunking_type: match &reference.chunking_type {
ChunkingType::Parallel { hoisted: true, .. } => "hoisted".to_string(),
ChunkingType::Parallel { hoisted: false, .. } => "sync".to_string(),
ChunkingType::Async => "async".to_string(),
ChunkingType::Isolated {
merge_tag: None, ..
} => "isolated".to_string(),
ChunkingType::Isolated {
merge_tag: Some(name),
..
} => format!("isolated {name}"),
ChunkingType::Shared {
merge_tag: None, ..
} => "shared".to_string(),
ChunkingType::Shared {
merge_tag: Some(name),
..
} => format!("shared {name}"),
ChunkingType::Traced => "traced".to_string(),
},
}
}
}

#[napi(object)]
pub struct NapiModuleInfo {
pub ident: RcStr,
pub path: RcStr,
pub depth: u32,
pub size: u32,
pub retained_size: u32,
pub references: Vec<NapiModuleReference>,
pub incoming_references: Vec<NapiModuleReference>,
}

impl From<&ModuleInfo> for NapiModuleInfo {
fn from(info: &ModuleInfo) -> Self {
Self {
ident: info.ident.clone(),
path: info.path.clone(),
depth: info.depth,
size: info.size,
retained_size: info.retained_size,
references: info
.references
.iter()
.map(NapiModuleReference::from)
.collect(),
incoming_references: info
.incoming_references
.iter()
.map(NapiModuleReference::from)
.collect(),
}
}
}

#[napi(object)]
#[derive(Default)]
pub struct NapiModuleGraphSnapshot {
pub modules: Vec<NapiModuleInfo>,
pub entries: Vec<u32>,
}

impl From<&ModuleGraphSnapshot> for NapiModuleGraphSnapshot {
fn from(snapshot: &ModuleGraphSnapshot) -> Self {
Self {
modules: snapshot.modules.iter().map(NapiModuleInfo::from).collect(),
entries: snapshot
.entries
.iter()
.map(|&i| {
// If you have more that 4294967295 entries, you probably have other problems...
i.try_into().unwrap()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potential panic due to integer overflow when converting module entry indices from usize to u32.

View Details

Analysis

The code converts usize values to u32 using try_into().unwrap() without proper error handling. On 64-bit systems, if there are more than 4,294,967,295 module entries, the conversion will fail and cause a panic. While the comment acknowledges this is unlikely, using unwrap() creates a potential crash point.

The ModuleGraphSnapshot.entries field is Vec<usize> but NapiModuleGraphSnapshot.entries is Vec<u32>. This size mismatch could cause runtime panics when processing large module graphs.


Recommendation

Replace try_into().unwrap() with proper error handling:

.map(|&i| {
    i.try_into().unwrap_or_else(|_| {
        // Log error and use max value as fallback
        tracing::warn!("Module entry index {} exceeds u32::MAX, using u32::MAX", i);
        u32::MAX
    })
})

Or consider changing the NAPI interface to use larger integer types if needed.


👍 or 👎 to improve Vade.

})
.collect(),
}
}
}
105 changes: 104 additions & 1 deletion crates/napi/src/next_api/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use napi::{
};
use next_api::{
entrypoints::Entrypoints,
module_graph_snapshot::{ModuleGraphSnapshot, get_module_graph_snapshot},
operation::{
EntrypointsOperation, InstrumentationOperation, MiddlewareOperation, OptionEndpoint,
RouteOperation,
Expand Down Expand Up @@ -63,13 +64,14 @@ use url::Url;
use crate::{
next_api::{
endpoint::ExternalEndpoint,
module_graph::NapiModuleGraphSnapshot,
turbopack_ctx::{
NapiNextTurbopackCallbacks, NapiNextTurbopackCallbacksJsObject, NextTurboTasks,
NextTurbopackContext, create_turbo_tasks,
},
utils::{
DetachedVc, NapiDiagnostic, NapiIssue, RootTask, TurbopackResult, get_diagnostics,
get_issues, subscribe,
get_issues, strongly_consistent_catch_collectables, subscribe,
},
},
register,
Expand Down Expand Up @@ -987,6 +989,40 @@ async fn output_assets_operation(
Ok(Vc::cell(output_assets.into_iter().collect()))
}

#[napi]
pub async fn project_entrypoints(
#[napi(ts_arg_type = "{ __napiType: \"Project\" }")] project: External<ProjectInstance>,
) -> napi::Result<TurbopackResult<NapiEntrypoints>> {
let container = project.container;

let (entrypoints, issues, diags) = project
.turbopack_ctx
.turbo_tasks()
.run_once(async move {
let entrypoints_with_issues_op = get_entrypoints_with_issues_operation(container);

// Read and compile the files
let EntrypointsWithIssues {
entrypoints,
issues,
diagnostics,
effects: _,
} = &*entrypoints_with_issues_op
.read_strongly_consistent()
.await?;

Ok((entrypoints.clone(), issues.clone(), diagnostics.clone()))
})
.await
.map_err(|e| napi::Error::from_reason(PrettyPrintError(&e).to_string()))?;

Ok(TurbopackResult {
result: NapiEntrypoints::from_entrypoints_op(&entrypoints, &project.turbopack_ctx)?,
issues: issues.iter().map(|i| NapiIssue::from(&**i)).collect(),
diagnostics: diags.iter().map(|d| NapiDiagnostic::from(d)).collect(),
})
}

#[napi(ts_return_type = "{ __napiType: \"RootTask\" }")]
pub fn project_entrypoints_subscribe(
#[napi(ts_arg_type = "{ __napiType: \"Project\" }")] project: External<ProjectInstance>,
Expand Down Expand Up @@ -1650,3 +1686,70 @@ pub fn project_get_source_map_sync(
tokio::runtime::Handle::current().block_on(project_get_source_map(project, file_path))
})
}

#[napi]
pub async fn project_module_graph(
#[napi(ts_arg_type = "{ __napiType: \"Project\" }")] project: External<ProjectInstance>,
) -> napi::Result<TurbopackResult<NapiModuleGraphSnapshot>> {
let container = project.container;
let (module_graph, issues, diagnostics) = project
.turbopack_ctx
.turbo_tasks()
.run_once(async move {
let module_graph_op = get_module_graph_with_issues_operation(container);
let ModuleGraphWithIssues {
module_graph,
issues,
diagnostics,
effects: _,
} = &*module_graph_op.connect().await?;
Ok((module_graph.clone(), issues.clone(), diagnostics.clone()))
})
.await
.map_err(|e| napi::Error::from_reason(PrettyPrintError(&e).to_string()))?;

Ok(TurbopackResult {
result: module_graph.map_or_else(NapiModuleGraphSnapshot::default, |m| {
NapiModuleGraphSnapshot::from(&*m)
}),
issues: issues.iter().map(|i| NapiIssue::from(&**i)).collect(),
diagnostics: diagnostics
.iter()
.map(|d| NapiDiagnostic::from(d))
.collect(),
})
}

#[turbo_tasks::value(serialization = "none")]
struct ModuleGraphWithIssues {
module_graph: Option<ReadRef<ModuleGraphSnapshot>>,
issues: Arc<Vec<ReadRef<PlainIssue>>>,
diagnostics: Arc<Vec<ReadRef<PlainDiagnostic>>>,
effects: Arc<Effects>,
}

#[turbo_tasks::function(operation)]
async fn get_module_graph_with_issues_operation(
project: ResolvedVc<ProjectContainer>,
) -> Result<Vc<ModuleGraphWithIssues>> {
let module_graph_op = get_module_graph_operation(project);
let (module_graph, issues, diagnostics, effects) =
strongly_consistent_catch_collectables(module_graph_op).await?;
Ok(ModuleGraphWithIssues {
module_graph,
issues,
diagnostics,
effects,
}
.cell())
}

#[turbo_tasks::function(operation)]
async fn get_module_graph_operation(
project: ResolvedVc<ProjectContainer>,
) -> Result<Vc<ModuleGraphSnapshot>> {
let project = project.project();
let graph = project.whole_app_module_graphs().await?.full;
let snapshot = get_module_graph_snapshot(*graph, None).resolve().await?;
Ok(snapshot)
}
Loading
Loading