This document provides comprehensive API documentation for the MCP Vala library, covering all public classes, interfaces, and methods.
The main server class that orchestrates all MCP functionality.
public Server (
Mcp.Types.Protocol.ServerInfo server_info,
Mcp.Types.Protocol.ServerCapabilities capabilities
)
Parameters:
server_info: Information about the server (name, version, description)capabilities: Server capabilities (resources, tools, prompts, logging)public Mcp.Resources.Manager resource_manager { get; }
public Mcp.Tools.Manager tool_manager { get; }
public Mcp.Prompts.Manager prompt_manager { get; }
public bool is_running { get; }
public bool is_initialized { get; }
public signal void initialized ();
public signal void shutdown ();
public async bool start () throws Error
public async void stop ()
public void send_notification (string method, Variant? params = null)
// Create server
var server_info = new Mcp.Types.Protocol.ServerInfo (
"my-server",
"1.0.0",
"My MCP server"
);
var capabilities = new Mcp.Types.Protocol.ServerCapabilities ();
capabilities.logging = true;
capabilities.tools = new Mcp.Types.Protocol.ToolsCapabilities ();
var server = new Mcp.Core.Server (server_info, capabilities);
// Start server
bool started = yield server.start ();
if (started) {
print ("Server started successfully\n");
}
The library now uses Jsonrpc.Server from jsonrpc-glib-1.0 for handling JSON-RPC communication. The custom MessageHandler and StdioTransport classes have been removed in favor of the standard implementation.
Error domain for MCP-specific errors.
public static const int PARSE_ERROR = -32700;
public static const int INVALID_REQUEST = -32600;
public static const int METHOD_NOT_FOUND = -32601;
public static const int INVALID_PARAMS = -32602;
public static const int INTERNAL_ERROR = -32603;
public static const int NOT_FOUND = -32604;
public static const int INVALID_ARGUMENT = -32605;
public static Error PARSE (string message)
public static Error INVALID_REQUEST (string message)
public static Error METHOD_NOT_FOUND (string message)
public static Error INVALID_PARAMS (string message)
public static Error INTERNAL_ERROR (string message)
public static Error NOT_FOUND (string message)
public static Error INVALID_ARGUMENT (string message)
Utility class for injecting Content-Length headers into messages.
public static string process (string message)
Injects the Content-Length header into a JSON-RPC message.
Utility class for stripping Content-Length headers from messages.
public static string process (string message)
Removes the Content-Length header from a JSON-RPC message.
Information about the MCP server.
public ServerInfo (
string name,
string version,
string? description = null,
string? website_url = null
)
public string name { get; set; }
public string version { get; set; }
public string? description { get; set; }
public string? website_url { get; set; }
Server capabilities configuration.
public bool logging { get; set; }
public Mcp.Types.Protocol.CompletionsCapabilities? completions { get; set; }
public Mcp.Types.Protocol.ResourcesCapabilities? resources { get; set; }
public Mcp.Types.Protocol.ToolsCapabilities? tools { get; set; }
public Mcp.Types.Protocol.PromptsCapabilities? prompts { get; set; }
Completion-related capabilities.
public bool list_changed { get; set; }
Resource-related capabilities.
public bool subscribe { get; set; }
public bool list_changed { get; set; }
Tool-related capabilities.
public bool list_changed { get; set; }
Prompt-related capabilities.
public bool list_changed { get; set; }
The library now uses Jsonrpc.Server from jsonrpc-glib-1.0 for handling JSON-RPC communication. Custom JSON-RPC message types have been removed in favor of the standard implementation.
The library internally converts between Json.Node (used for MCP protocol types) and GLib.Variant (used by Jsonrpc.Server) for seamless integration.
Errors are now handled using Jsonrpc.Error for proper JSON-RPC 2.0 error responses.
Common content types used across the MCP protocol.
Base interface for content blocks.
public class TextContent : Mcp.Types.Common.ContentBlock
public class ImageContent : Mcp.Types.Common.ContentBlock
public class ResourceContents : Mcp.Types.Common.ContentBlock
Text content block.
public TextContent (string text)
public string text { get; construct; }
public string type { get; default = "text"; }
Image content block.
public ImageContent (string data, string mime_type)
public string data { get; construct; }
public string mime_type { get; construct; }
public string type { get; default = "image"; }
Base interface for resource contents.
public class TextResourceContents : Mcp.Types.Common.ResourceContents
public class BlobResourceContents : Mcp.Types.Common.ResourceContents
Text-based resource contents.
public TextResourceContents (string uri, string text)
public string uri { get; construct; }
public string text { get; construct; }
public string mime_type { get; default = "text/plain"; }
Binary resource contents.
public BlobResourceContents (string uri, uint8[] data, string? mime_type = null)
public string uri { get; construct; }
public uint8[] data { get; construct; }
public string mime_type { get; construct; }
Resource annotations.
public double? priority { get; set; }
public string? audience { get; set; }
public bool? expire_time { get; set; }
Utility functions for working with GLib.Variant data structures.
public static VariantBuilder new_dict_builder ()
public static void add_string (VariantBuilder builder, string key, string value)
public static void add_int (VariantBuilder builder, string key, int value)
public static void add_bool (VariantBuilder builder, string key, bool value)
public static void add_double (VariantBuilder builder, string key, double value)
public static void add_variant (VariantBuilder builder, string key, Variant value)
public static bool has_key (Variant dict, string key)
public static string get_string (Variant dict, string key, string? default_value = null)
public static int get_int (Variant dict, string key, int default_value = 0)
public static bool get_bool (Variant dict, string key, bool default_value = false)
public static double get_double (Variant dict, string key, double default_value = 0.0)
public static Variant? get_variant (Variant dict, string key)
public static string[] get_string_array (Variant dict, string key)
public static VariantBuilder new_array_builder (string element_type)
public static void add_string_element (VariantBuilder builder, string value)
public static void add_int_element (VariantBuilder builder, int value)
public static void add_bool_element (VariantBuilder builder, bool value)
public static void add_double_element (VariantBuilder builder, double value)
public static void add_variant_element (VariantBuilder builder, Variant value)
public static int get_array_size (Variant array)
public static string get_string_element (Variant array, int index)
public static int get_int_element (Variant array, int index)
public static bool get_bool_element (Variant array, int index)
public static double get_double_element (Variant array, int index)
public static Variant get_variant_element (Variant array, int index)
public static Json.Node? to_json (Variant variant)
public static Variant? from_json (Json.Node node)
Interface for resource providers.
public abstract async Gee.ArrayList<Mcp.Resources.Types.Resource> list_resources (string? cursor) throws Error
public abstract async Mcp.Types.Common.ResourceContents read_resource (string uri) throws Error
public virtual async void subscribe (string uri) throws Error
public virtual async void unsubscribe (string uri) throws Error
Base implementation for resource providers.
protected BaseProvider ()
public signal void resource_updated (string uri);
public Gee.ArrayList<string> get_subscriptions ()
public bool is_subscribed (string uri)
public void notify_resource_updated (string uri)
protected virtual Mcp.Resources.Types.Resource? get_resource_metadata (string uri)
protected string guess_mime_type (string uri)
Manages resource providers and handles resource operations.
public void register_provider (string name, Mcp.Resources.Provider provider)
public void unregister_provider (string name)
public async Gee.ArrayList<Mcp.Resources.Types.Resource> list_resources (string? cursor) throws Error
public async Mcp.Types.Common.ResourceContents read_resource (string uri) throws Error
public async void subscribe (string uri) throws Error
public async void unsubscribe (string uri) throws Error
public async Gee.ArrayList<Mcp.Resources.Types.ResourceTemplate> list_templates () throws Error
public void register_template (string name, Mcp.Resources.Types.ResourceTemplate template)
public void unregister_template (string name)
Resource definition.
public Resource (string uri, string name)
public string uri { get; set; }
public string name { get; set; }
public string? title { get; set; }
public string? description { get; set; }
public string? mime_type { get; set; }
public Mcp.Types.Common.Annotations? annotations { get; set; }
public int64? size { get; set; }
public Json.Node to_json ()
public Resource.from_json (Json.Node node) throws Error
Resource template for dynamic resource creation.
public ResourceTemplate (string uri_template, string name)
public string uri_template { get; set; }
public string name { get; set; }
public string? title { get; set; }
public string? description { get; set; }
public string? mime_type { get; set; }
public Mcp.Types.Common.Annotations? annotations { get; set; }
Interface for tool executors.
public abstract Mcp.Tools.Types.ToolDefinition get_definition () throws Error
public abstract async Mcp.Tools.Types.CallToolResult execute (Variant arguments) throws Error
Base implementation for tool executors.
protected BaseExecutor (Mcp.Tools.Types.ToolDefinition definition)
public virtual Mcp.Tools.Types.ToolDefinition get_definition () throws Error
public async Mcp.Tools.Types.CallToolResult execute (Variant arguments) throws Error
protected virtual void validate_arguments (Variant arguments) throws Error
protected Mcp.Tools.Types.CallToolResult create_text_result (string text)
protected Mcp.Tools.Types.CallToolResult create_text_results (string[] texts)
protected Mcp.Tools.Types.CallToolResult create_error_result (Error error)
protected Mcp.Tools.Types.CallToolResult create_structured_result (string text, Variant structured_data)
protected string? get_string_arg (Variant arguments, string name, string? default_value = null)
protected bool get_bool_arg (Variant arguments, string name, bool default_value = false)
protected int get_int_arg (Variant arguments, string name, int default_value = 0)
protected double get_double_arg (Variant arguments, string name, double default_value = 0.0)
protected abstract async Mcp.Tools.Types.CallToolResult do_execute (Variant arguments) throws Error
Manages tool executors and handles tool operations.
public void register_executor (string name, Mcp.Tools.Executor executor)
public void unregister_executor (string name)
public async Gee.ArrayList<Mcp.Tools.Types.ToolDefinition> list_tools () throws Error
public async Mcp.Tools.Types.CallToolResult call_tool (string name, Variant arguments) throws Error
Tool definition.
public ToolDefinition (string name, Variant input_schema)
public string name { get; set; }
public string? title { get; set; }
public string? description { get; set; }
public Variant input_schema { get; set; }
public Variant? output_schema { get; set; }
public Mcp.Tools.Types.ToolExecution? execution { get; set; }
public Mcp.Tools.Types.ToolAnnotations? annotations { get; set; }
public Json.Node to_json ()
public ToolDefinition.from_json (Json.Node node) throws Error
Result of calling a tool.
public CallToolResult ()
public CallToolResult (Gee.ArrayList<Mcp.Types.Common.ContentBlock> content)
public Gee.ArrayList<Mcp.Types.Common.ContentBlock> content { get; set; }
public Variant? structured_content { get; set; }
public bool is_error { get; set; }
public Json.Node to_json ()
public CallToolResult.from_json (Json.Node node) throws Error
Context for tool execution with progress tracking.
public ToolExecutionContext (string tool_name, Variant arguments)
public string tool_name { get; construct; }
public Variant arguments { get; construct; }
public bool supports_progress { get; set; }
public bool task_support { get; set; }
public void report_progress (Mcp.Tools.Types.ToolProgress progress)
public void add_annotation (Mcp.Tools.Types.ToolAnnotation annotation)
Progress information for long-running tools.
public ToolProgress (double progress, string? message = null)
public double progress { get; set; }
public string? message { get; set; }
Tool execution annotations.
public ToolAnnotations ()
public string? audience { get; set; }
public double? priority { get; set; }
public Gee.ArrayList<Mcp.Tools.Types.ToolAnnotation> annotations { get; set; }
Individual tool annotation.
public ToolAnnotation (string type, Variant data)
public string type { get; set; }
public Variant data { get; set; }
Tool execution settings.
public ToolExecution (string task_support = "forbidden")
public string task_support { get; set; }
public bool supports_progress { get; set; }
Interface for prompt templates.
public abstract Mcp.Prompts.Types.PromptDefinition get_definition ()
public abstract async Mcp.Prompts.Types.GetPromptResult get_prompt (Variant arguments) throws Error
Base implementation for prompt templates.
protected BaseTemplate (string name, string? title = null, string? description = null)
protected BaseTemplate (string name, string? title, string? description, Gee.ArrayList<Mcp.Prompts.Types.PromptArgument> arguments)
public virtual Mcp.Prompts.Types.PromptDefinition get_definition ()
public virtual async Mcp.Prompts.Types.GetPromptResult get_prompt (Variant arguments) throws Error
protected virtual void validate_arguments (Variant arguments) throws Error
protected virtual Gee.ArrayList<Mcp.Prompts.Types.PromptMessage> generate_messages (Variant arguments)
protected void add_argument (string name, string? title = null, string? description = null, bool required = false)
protected string substitute_variables (string template, Variant arguments)
protected Mcp.Types.Common.TextContent create_text_content (string template, Variant arguments)
protected Mcp.Prompts.Types.PromptMessage create_user_message (string template, Variant arguments)
protected Mcp.Prompts.Types.PromptMessage create_assistant_message (string template, Variant arguments)
Manages prompt templates and handles prompt operations.
public void register_template (string name, Mcp.Prompts.Template template)
public void unregister_template (string name)
public async Gee.ArrayList<Mcp.Prompts.Types.PromptDefinition> list_prompts () throws Error
public async Mcp.Prompts.Types.GetPromptResult get_prompt (string name, Variant arguments) throws Error
Prompt definition.
public PromptDefinition (string name)
public string name { get; set; }
public string? title { get; set; }
public string? description { get; set; }
public Gee.ArrayList<Mcp.Prompts.Types.PromptArgument> arguments { get; set; }
public Json.Node to_json ()
public PromptDefinition.from_json (Json.Node node) throws Error
Prompt argument definition.
public PromptArgument (string name)
public string name { get; set; }
public string? title { get; set; }
public string? description { get; set; }
public bool required { get; set; }
public Json.Node to_json ()
public PromptArgument.from_json (Json.Node node) throws Error
Result of getting a prompt.
public GetPromptResult ()
public string? description { get; set; }
public Gee.ArrayList<Mcp.Prompts.Types.PromptMessage> messages { get; set; }
public Json.Node to_json ()
public GetPromptResult.from_json (Json.Node node) throws Error
Prompt message.
public PromptMessage (string role, Mcp.Types.Common.ContentBlock content)
public string role { get; set; }
public Mcp.Types.Common.ContentBlock content { get; set; }
public Json.Node to_json ()
public PromptMessage.from_json (Json.Node node) throws Error
public class MyMcpServer : GLib.Object {
private Mcp.Core.Server server;
public MyMcpServer () {
// 1. Create server info
var server_info = new Mcp.Types.Protocol.ServerInfo (
"my-server",
"1.0.0",
"Description of my server"
);
// 2. Configure capabilities
var capabilities = new Mcp.Types.Protocol.ServerCapabilities ();
capabilities.logging = true;
// 3. Create server
server = new Mcp.Core.Server (server_info, capabilities);
// 4. Set up components
setup_components ();
}
private void setup_components () {
// Register resources, tools, and prompts
}
public async int run () {
// Start server and run main loop
yield server.start ();
new MainLoop ().run ();
return 0;
}
}
public class MyResourceProvider : Mcp.Resources.BaseProvider {
private HashTable<string, string> storage;
public MyResourceProvider () {
storage = new HashTable<string, string> (str_hash, str_equal);
initialize_data ();
}
public override async Gee.ArrayList<Mcp.Resources.Types.Resource> list_resources (string? cursor) throws Error {
var resources = new Gee.ArrayList<Mcp.Resources.Types.Resource> ();
foreach (var entry in storage.get_keys ()) {
var resource = new Mcp.Resources.Types.Resource (entry, entry);
resource.mime_type = guess_mime_type (entry);
resources.add (resource);
}
return resources;
}
public override async Mcp.Types.Common.ResourceContents read_resource (string uri) throws Error {
if (!storage.contains (uri)) {
throw new Mcp.Core.Error.NOT_FOUND ("Resource not found");
}
return new Mcp.Types.Common.TextResourceContents (uri, storage[uri]);
}
}
public class MyTool : Mcp.Tools.BaseExecutor {
public MyTool () {
// Define input schema using Variant
var input_schema = Mcp.Types.VariantUtils.new_dict_builder ();
input_schema.add ("{sv}", "type", new Variant.string ("object"));
var properties = Mcp.Types.VariantUtils.new_dict_builder ();
var param = Mcp.Types.VariantUtils.new_dict_builder ();
param.add ("{sv}", "type", new Variant.string ("string"));
param.add ("{sv}", "description", new Variant.string ("Parameter description"));
properties.add ("{sv}", "param", param.end ());
var required = new VariantBuilder (new VariantType ("as"));
required.add_value ("s", "param");
input_schema.add ("{sv}", "properties", properties.end ());
input_schema.add ("{sv}", "required", required.end ());
var definition = new Mcp.Tools.Types.ToolDefinition ("my_tool", input_schema.end ());
definition.description = "Description of my tool";
base (definition);
}
protected override async Mcp.Tools.Types.CallToolResult do_execute (Variant arguments) throws Error {
string param = get_string_arg (arguments, "param");
// Process input
string result = process_parameter (param);
return create_text_result ("Result: %s".printf (result));
}
private string process_parameter (string input) {
// Custom processing logic
return input.up ();
}
}
public class MyPrompt : Mcp.Prompts.BaseTemplate {
public MyPrompt () {
var arguments = new Gee.ArrayList<Mcp.Prompts.Types.PromptArgument> ();
var topic_arg = new Mcp.Prompts.Types.PromptArgument ("topic");
topic_arg.description = "The topic to generate content for";
topic_arg.required = true;
arguments.add (topic_arg);
var style_arg = new Mcp.Prompts.Types.PromptArgument ("style");
style_arg.description = "Writing style";
arguments.add (style_arg);
base ("my_prompt", "My Prompt", "Description of my prompt", arguments);
}
protected override async Mcp.Prompts.Types.GetPromptResult do_render (Variant arguments) throws Error {
string topic = Mcp.Types.VariantUtils.get_string (arguments, "topic");
string style = Mcp.Types.VariantUtils.get_string (arguments, "style", "neutral");
string template = """Generate %s content about %s.""".printf (style, topic);
var content = new Mcp.Types.Common.TextContent (template);
var message = new Mcp.Prompts.Types.PromptMessage ("user", content);
var messages = new Gee.ArrayList<Mcp.Prompts.Types.PromptMessage> ();
messages.add (message);
return new Mcp.Prompts.Types.GetPromptResult (messages);
}
}
The library implements the JSON-RPC 2.0 specification for MCP:
{
"jsonrpc": "2.0",
"id": "unique-request-id",
"method": "tools/list",
"params": {}
}
{
"jsonrpc": "2.0",
"id": "unique-request-id",
"result": {
"tools": [...]
}
}
{
"jsonrpc": "2.0",
"id": "unique-request-id",
"error": {
"code": -32602,
"message": "Invalid params",
"data": null
}
}
{
"jsonrpc": "2.0",
"method": "notifications/resources/list_changed",
"params": {}
}
The library uses Vala's type system for compile-time safety:
string - Text dataint/int64 - Integer valuesdouble - Floating-point valuesbool - Boolean valuesGee.ArrayList<T> - Dynamic arraysHashTable<K,V> - Key-value mapsJson.Object - JSON objectsJson.Array - JSON arraysJson.Node - Generic JSON valueOptional types use ? suffix:
public string? description { get; set; } // Can be null
public string name { get; set; } // Cannot be null
Mcp.Core.McpError
├── PARSE_ERROR (-32700)
├── INVALID_REQUEST (-32600)
├── METHOD_NOT_FOUND (-32601)
├── INVALID_PARAMS (-32602)
├── INTERNAL_ERROR (-32603)
├── RESOURCE_NOT_FOUND (-32604)
└── TRANSPORT_ERROR (-32605)
try {
// MCP operation
} catch (Mcp.Core.Error e) {
// Handle MCP-specific errors
stderr.printf ("MCP Error [%d]: %s\n", e.code, e.message);
} catch (GLib.Error e) {
// Handle other errors
stderr.printf ("GLib Error: %s\n", e.message);
}
The library follows MCP specification version "2025-11-25" and maintains backward compatibility where possible. Future versions will be additive and non-breaking.
If you were using custom JSON-RPC handling in previous versions:
The migration is transparent for existing code using the library's public API.