Functions Reference

Built-in functions available in Rhai scripts


Apollo MCP Server provides several built-in function namespaces in the Rhai scripting environment. These functions are available in all Rhai scripts without any imports.

Env

Read environment variables from the server process.

FunctionDescriptionExample
Env::get(name)Returns the value of an environment variable or an empty string if the variable isn't set.let key = Env::get("API_KEY");
note
When an environment variable isn't set, Env::get logs a warning and returns an empty string; it doesn't throw an error.
Rhai
1let api_key = Env::get("API_KEY");
2let region = Env::get("AWS_REGION");
3
4fn on_execute_graphql_operation(ctx) {
5    ctx.headers["x-api-key"] = api_key;
6}

JSON

Parse and serialize JSON data.

FunctionDescriptionExample
JSON::parse(input)Parses a JSON string into a Rhai value (for example, a map, array, string, or number). Throws on invalid JSON.let obj = JSON::parse("{\"key\": \"value\"}");
JSON::stringify(value)Converts a Rhai value into a JSON string.let s = JSON::stringify(#{name: "apollo"});
Rhai
1// Parse a JSON string into an object
2let data = JSON::parse("{\"name\": \"Apollo\", \"version\": 1}");
3print(data.name);    // "Apollo"
4print(data.version); // 1
5
6// Convert a Rhai map to a JSON string
7let obj = #{status: "ok", count: 42};
8let json_string = JSON::stringify(obj);
9print(json_string);  // {"count":42,"status":"ok"}

Sha256

Compute SHA-256 cryptographic hashes.

FunctionDescriptionExample
Sha256::digest(input)Returns the hex-encoded SHA-256 hash of an input string.let hash = Sha256::digest("hello");
Rhai
1let hash = Sha256::digest("hello");
2// hash = "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"

Regex

Perform regular-expression operations on strings.

FunctionDescriptionExample
Regex::is_match(string, pattern)Returns true if the pattern matches anywhere in the string. Throws on invalid pattern.Regex::is_match("hello123", "\\d+")
Regex::replace(string, pattern, replacement)Replaces all matching patterns with the replacement string. Supports numbered ($1) and named ($name) capture groups. Throws on invalid pattern.Regex::replace("foo bar", "foo", "baz")
Regex::matches(string, pattern)Returns an array of all matching substrings or an empty array if there are no matches. Throws on invalid pattern.Regex::matches("abc 123 def 456", "\\d+")
Rhai
1// Check if a string contains digits
2let has_numbers = Regex::is_match("order-123", "\\d+");
3// has_numbers = true
4
5// Replace all matches
6let result = Regex::replace("foo bar foo", "foo", "baz");
7// result = "baz bar baz"
8
9// Use capture groups to reformat a date
10let reformatted = Regex::replace("2025-01-15", "(\\d{4})-(\\d{2})-(\\d{2})", "$2/$3/$1");
11// reformatted = "01/15/2025"
12
13// Use named capture groups
14let swapped = Regex::replace("John Smith", "(?P<first>\\w+) (?P<last>\\w+)", "$last, $first");
15// swapped = "Smith, John"
16
17// Find all matches
18let numbers = Regex::matches("abc 123 def 456", "\\d+");
19// numbers = ["123", "456"]

ErrorCode

These are the constants for structured error responses when using throw in lifecycle hooks. Go to Lifecycle Hooks for details about error handling.

ConstantDescription
ErrorCode::INVALID_REQUESTIndicates a client error, like a missing header or invalid input.
ErrorCode::INTERNAL_ERRORIndicates a server-side error. This is the default when no other error code is specified.
Rhai
1fn on_execute_graphql_operation(ctx) {
2    if ctx.incoming_request.headers["authorization"] == "" {
3        throw #{
4            message: "Authorization required",
5            code: ErrorCode::INVALID_REQUEST
6        };
7    }
8}
Feedback

Edit on GitHub

Ask Community