Skip to main content

validate_server_id_slug

Function validate_server_id_slug 

Source
pub fn validate_server_id_slug(id: &str) -> Result<(), ServerIdSlugError>
Expand description

Validates that id is a filesystem-safe server id slug: 1-64 lowercase ASCII letters, digits, or hyphens (^[a-z0-9-]+$).

This is a stricter, opt-in invariant layered on top of ServerId::new’s own baseline (single non-empty path segment, no .. or path separator): every slug-shaped id is automatically a valid ServerId, but not every valid ServerId is slug-shaped — e.g. a raw mcp.json key like claude_ai_Gmail (mixed case, underscore) is a legitimate ServerId but not a valid slug. ServerId::new deliberately does not enforce this tighter rule itself, since callers that only need a safe path segment (e.g. looking up an existing mcp.json entry) must keep accepting non-slug-shaped ids. Callers that need the id to become a directory name or generated-code identifier — where entry validation and filesystem confinement must agree on the exact same rule — should call this function too, in addition to ServerId::new.

§Errors

Returns ServerIdSlugError if id is empty, exceeds MAX_SERVER_ID_LENGTH bytes, or contains a character other than a lowercase ASCII letter, digit, or hyphen.

§Examples

use mcp_execution_core::validate_server_id_slug;

assert!(validate_server_id_slug("github").is_ok());
assert!(validate_server_id_slug("my-server-123").is_ok());
assert!(validate_server_id_slug("").is_err());
assert!(validate_server_id_slug("GitHub").is_err());
assert!(validate_server_id_slug("my_server").is_err());