Skip to content
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
7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ rust-version = "1.90.0"
authors = ["Roman Emreis <[email protected]>"]
repository = "https://github.com/RomanEmreis/neva"
documentation = "https://docs.rs/neva"
keywords = ["modelcontextprotocol", "mcp", "sdk", "ai", "llm"]
categories = [
"network-programming",
"asynchronous",
"api-bindings",
"web-programming"
]

[workspace.dependencies]
neva_macros = { path = "neva_macros", version = "0.3.0" }
Expand Down
4 changes: 3 additions & 1 deletion examples/elicitation/server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ fn format_contact(c: Contact) -> String {
#[tokio::main]
async fn main() {
App::new()
.with_options(|opt| opt.with_stdio())
.with_options(|opt| opt
.with_name("Elicitation Example Server")
.with_stdio())
.run().await;
}
1 change: 1 addition & 0 deletions examples/http/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ async fn main() {
App::new()
.with_options(|opt| {
opt.with_http(|http| http.bind("127.0.0.1:3000").with_endpoint("/mcp"))
.with_name("Streamable HTTP Example Server")
.with_logging(handle)
})
.run()
Expand Down
5 changes: 4 additions & 1 deletion examples/large_resources_server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ fn get_res_info(uri: Uri, name: String) -> Resource {
#[tokio::main]
async fn main() {
App::new()
.with_options(|opt| opt.with_default_http())
.with_options(|opt| {
opt.with_name("Large resource example server")
.with_default_http()
})
.run()
.await;
}
1 change: 1 addition & 0 deletions examples/logging/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ async fn main() {
.with_options(|opt| {
opt.with_stdio()
.with_mcp_version("2024-11-05")
.with_name("Logging Example Server")
.with_logging(handle)
})
.run()
Expand Down
6 changes: 5 additions & 1 deletion examples/middlewares/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ async fn main() {
.init();

App::new()
.with_options(|opt| opt.with_stdio().with_logging(handle))
.with_options(|opt| {
opt.with_name("Middleware Example Server")
.with_stdio()
.with_logging(handle)
})
.wrap(logging_middleware) // Wraps all requests that pass through the server
.wrap_tools(global_tool_middleware) // Wraps all tools/call requests that pass through the server
.run()
Expand Down
5 changes: 4 additions & 1 deletion examples/pagination/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ async fn filter_resources(
#[tokio::main]
async fn main() {
App::new()
.with_options(|opt| opt.with_http(|http| http.bind("127.0.0.1:3000")))
.with_options(|opt| {
opt.with_name("Pagination Example Server")
.with_http(|http| http.bind("127.0.0.1:3000"))
})
.add_singleton(ResourcesRepository::new())
.run()
.await;
Expand Down
6 changes: 5 additions & 1 deletion examples/progress/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ async fn main() {
.init();

App::new()
.with_options(|opt| opt.with_tasks(|tasks| tasks.with_all()).with_default_http())
.with_options(|opt| {
opt.with_name("Progress Example Server")
.with_tasks(|tasks| tasks.with_all())
.with_default_http()
})
.run()
.await;
}
17 changes: 9 additions & 8 deletions examples/protected-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,16 @@ async fn main() {

App::new()
.with_options(|opt| {
opt.with_http(|http| {
http.with_auth(|auth| {
auth.validate_exp(false)
.with_aud(["some aud"])
.with_iss(["some issuer"])
.set_decoding_key(secret.as_bytes())
opt.with_name("Protected Server Example")
.with_http(|http| {
http.with_auth(|auth| {
auth.validate_exp(false)
.with_aud(["some aud"])
.with_iss(["some issuer"])
.set_decoding_key(secret.as_bytes())
})
})
})
.with_logging(handle)
.with_logging(handle)
})
.run()
.await;
Expand Down
4 changes: 3 additions & 1 deletion examples/roots/server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ async fn roots_request(mut ctx: Context) -> Result<String, Error> {
#[tokio::main]
async fn main() {
App::new()
.with_options(|opt| opt.with_default_http())
.with_options(|opt| opt
.with_name("Roots Example Server")
.with_default_http())
.run()
.await;
}
4 changes: 3 additions & 1 deletion examples/sampling/server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ async fn main() {
.set_decoding_key(secret.as_bytes()));

App::new()
.with_options(|opt| opt.set_http(http))
.with_options(|opt| opt
.with_name("Sampling Example Server")
.set_http(http))
.run()
.await;
}
Expand Down
1 change: 1 addition & 0 deletions examples/tasks/server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ fn main() {

App::new()
.with_options(|opt| opt
.with_name("Tasks Example Server")
.with_default_http()
.with_tasks(|t| t.with_all()))
.run_blocking();
Expand Down
3 changes: 2 additions & 1 deletion examples/updates/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ async fn get_resource(uri: Uri) -> ResourceContents {
#[tokio::main]
async fn main() {
let mut app = App::new().with_options(|opt| {
opt.with_stdio()
opt.with_name("Updates Example Server")
.with_stdio()
.with_resources(|res| res.with_subscribe().with_list_changed())
.with_mcp_version("2024-11-05")
});
Expand Down
4 changes: 2 additions & 2 deletions neva/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ authors.workspace = true
license.workspace = true
repository.workspace = true
documentation.workspace = true
categories = ["web-programming::http-server", "network-programming", "asynchronous"]
keywords = ["modelcontextprotocol", "mcp", "sdk", "ai", "framework"]
keywords.workspace = true
categories.workspace = true

[lints]
workspace = true
Expand Down
85 changes: 84 additions & 1 deletion neva/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ use {crate::types::notification::SetLevelRequestParams, tracing::Instrument};

mod collection;
pub mod context;
mod greeter;
pub(crate) mod handler;
pub mod options;

Expand All @@ -56,8 +57,10 @@ const DEFAULT_PAGE_SIZE: usize = 10;
type RequestHandlers = HashMap<String, RequestHandler<Response>>;

/// Represents an MCP server application
#[derive(Default)]
pub struct App {
/// Whether to print the startup greeting banner
greeting: bool,

/// MCP server options
pub(super) options: McpOptions,

Expand All @@ -76,10 +79,18 @@ impl Debug for App {
}
}

impl Default for App {
/// Creates a default [`App`] with all built-in handlers registered.
fn default() -> Self {
Self::new()
}
}

impl App {
/// Initializes a new MCP app
pub fn new() -> Self {
let mut app = Self {
greeting: cfg!(debug_assertions),
options: McpOptions::default(),
handlers: HashMap::new(),
#[cfg(feature = "di")]
Expand Down Expand Up @@ -196,6 +207,35 @@ impl App {
#[cfg(feature = "macros")]
self.register_methods();

// ORDERING CONSTRAINT: must execute after register_methods() so macro-registered
// tools/prompts are present; must execute before self.options.transport() consumes
// `proto` and before ServerRuntime::new() transitions collections to Runtime state
// (Collection::as_ref() panics if called in Runtime state).
if self.greeting {
let transport_label = self.options.transport_label();
let tools: Vec<String> = self.options.tools.as_ref().keys().cloned().collect();
let prompts: Vec<String> = self.options.prompts.as_ref().keys().cloned().collect();
let resource_templates: Vec<String> = self
.options
.resources_templates
.as_ref()
.keys()
.cloned()
.collect();

greeter::Greeter {
server_name: &self.options.implementation.name,
server_version: &self.options.implementation.version,
neva_version: env!("CARGO_PKG_VERSION"),
transport_label: &transport_label,
tools: &tools,
prompts: &prompts,
resource_templates: &resource_templates,
use_color: std::env::var_os("NO_COLOR").is_none(),
}
.print();
}

#[cfg(feature = "tracing")]
self.options
.add_middleware(make_mw(Self::tracing_middleware));
Expand Down Expand Up @@ -239,6 +279,36 @@ impl App {
}
}

/// Enable the greeting banner on startup (forced on, even in release builds).
///
/// # Example
/// ```no_run
/// use neva::App;
///
/// # fn main() {
/// let app = App::new().with_greeting();
/// # }
/// ```
pub fn with_greeting(mut self) -> Self {
self.greeting = true;
self
}

/// Suppress the greeting banner on startup.
///
/// # Example
/// ```no_run
/// use neva::App;
///
/// # fn main() {
/// let app = App::new().without_greeting();
/// # }
/// ```
pub fn without_greeting(mut self) -> Self {
self.greeting = false;
self
}

/// Configure MCP server options
pub fn with_options<F>(mut self, config: F) -> Self
where
Expand Down Expand Up @@ -939,8 +1009,21 @@ fn create_tracing_span(session_id: Option<uuid::Uuid>) -> tracing::Span {

#[cfg(test)]
mod tests {
use super::App;
use crate::types::{MessageBatch, MessageEnvelope};

#[test]
fn it_enables_greeting_with_with_greeting() {
let app = App::new().with_greeting();
assert!(app.greeting);
}

#[test]
fn it_disables_greeting_with_without_greeting() {
let app = App::new().without_greeting();
assert!(!app.greeting);
}

#[test]
fn batch_filtering_notifications_yield_no_response_slots() {
use crate::types::notification::Notification;
Expand Down
Loading
Loading