|
|
před 1 měsícem | |
|---|---|---|
| docs | před 1 měsícem | |
| examples | před 1 měsícem | |
| src | před 1 měsícem | |
| .gitignore | před 1 měsícem | |
| README.md | před 1 měsícem | |
| meson.build | před 1 měsícem |
A comprehensive Vala implementation of the Model Context Protocol (MCP) version "2025-11-25" using jsonrpc-gobject for STDIO communication.
The MCP Vala Library provides a complete implementation of the Model Context Protocol (MCP) in Vala, enabling developers to create MCP servers with minimal boilerplate code. MCP is a protocol that allows language models to securely interact with external resources, tools, and prompts through a standardized JSON-RPC interface.
This library offers a clean, type-safe, and asynchronous API that follows Vala's programming conventions while maintaining full compliance with the MCP specification.
# Clone the repository
git clone https://github.com/your-repo/mcp-vala.git
cd mcp-vala
# Configure and build
meson setup builddir
meson compile -C builddir
# Install (optional)
sudo meson install -C builddir
Create a minimal MCP server with just a few lines of code:
using Mcp;
int main (string[] args) {
// Create server info
var server_info = new Mcp.Types.Protocol.ServerInfo (
"minimal-server",
"1.0.0",
"A minimal MCP server"
);
// Create capabilities
var capabilities = new Mcp.Types.Protocol.ServerCapabilities ();
// Create and start server
var server = new Mcp.Core.Server (server_info, capabilities);
// Start the server (blocking)
bool started = yield server.start ();
// Keep the main loop running
var loop = new MainLoop ();
loop.run ();
return 0;
}
Compile and run:
valac --pkg mcp-vala --pkg json-glib-1.0 --pkg jsonrpc-glib-1.0 --pkg gio-unix-2.0 --pkg gee-0.8 minimal.vala -o minimal-server
./minimal-server
Resources represent data that can be read by clients, such as files, database entries, or API responses. The library provides:
Subscription support for resource change notifications
// Register a file system provider
var provider = new Mcp.Resources.Provider.FilesystemProvider ("/path/to/directory");
server.resource_manager.register_provider (provider);
Tools are functions that clients can execute, such as running commands, performing calculations, or interacting with external services.
public class CalculatorTool : Mcp.Tools.BaseExecutor {
public CalculatorTool () {
// 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 expr_prop = Mcp.Types.VariantUtils.new_dict_builder ();
expr_prop.add ("{sv}", "type", new Variant.string ("string"));
expr_prop.add ("{sv}", "description", new Variant.string ("Mathematical expression to evaluate"));
properties.add ("{sv}", "expression", expr_prop.end ());
var required = new VariantBuilder (new VariantType ("as"));
required.add_value ("s", "expression");
input_schema.add ("{sv}", "properties", properties.end ());
input_schema.add ("{sv}", "required", required.end ());
base (new Mcp.Tools.Types.ToolDefinition ("calculator", input_schema.end ()));
}
protected override async Mcp.Tools.Types.CallToolResult do_execute (Variant arguments) throws Error {
string expression = get_string_arg (arguments, "expression");
// Simple evaluation (in production, use a proper math parser)
string result = eval_expression (expression);
return create_text_result ("Result: %s".printf (result));
}
}
// Register the tool
server.tool_manager.register_executor (new CalculatorTool ());
Prompts are templates that can be filled with arguments to generate consistent messages for language models.
public class CodeReviewPrompt : Mcp.Prompts.BaseTemplate {
public CodeReviewPrompt () {
var arguments = new Gee.ArrayList<Mcp.Prompts.Types.PromptArgument> ();
var code_arg = new Mcp.Prompts.Types.PromptArgument ("code");
code_arg.description = "The code to review";
code_arg.required = true;
arguments.add (code_arg);
var style_arg = new Mcp.Prompts.Types.PromptArgument ("style");
style_arg.description = "Review style (strict, moderate, relaxed)";
arguments.add (style_arg);
base ("code_review", "Generate a code review for the provided code", arguments);
}
protected override async Mcp.Prompts.Types.GetPromptResult do_render (Variant arguments) throws Error {
string code = Mcp.Types.VariantUtils.get_string (arguments, "code");
string style = Mcp.Types.VariantUtils.has_key (arguments, "style")
? Mcp.Types.VariantUtils.get_string (arguments, "style")
: "moderate";
string prompt_text = @"Please review the following code using a $style style:\n\n$code\n\nProvide feedback on:\n1. Code quality\n2. Potential bugs\n3. Performance considerations\n4. Best practices";
var content = new Mcp.Types.Common.TextContent (prompt_text);
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);
}
}
// Register the prompt
server.prompt_manager.register_template (new CodeReviewPrompt ());
Mcp.Core.Server: Main server class that handles the MCP protocol using Jsonrpc.ServerMcp.Core.Error: Error domain and utilitiesMcp.Core.InjectContentLength: Utility for injecting Content-Length headers in messagesMcp.Core.StripContentLength: Utility for stripping Content-Length headers from messagesMcp.Resources.Manager: Manages resource providers and handles resource operationsMcp.Tools.Manager: Manages tool executors and handles tool operationsMcp.Prompts.Manager: Manages prompt templates and handles prompt operationsMcp.Resources.BaseProvider: Base implementation for resource providersMcp.Tools.BaseExecutor: Base implementation for tool executorsMcp.Prompts.BaseTemplate: Base implementation for prompt templatesMcp.Types.Protocol: Core protocol types (ServerInfo, Capabilities, etc.)Mcp.Types.Common: Common content types used across the protocolMcp.Types.VariantUtils: Utility functions for working with GLib.VariantMcp.Tools.Types.ToolExecutionContext: Context for tool execution with progress trackingMcp.Tools.Types.ToolProgress: Progress information for long-running toolsMcp.Tools.Types.ToolAnnotations: Annotations for tool execution results# Configure the build
meson setup builddir
# Build the library
meson compile -C builddir
# Run tests (when available)
meson test -C builddir
# Build examples
meson compile -C builddir examples
# Install
sudo meson install -C builddir
The library includes several examples that demonstrate different aspects of the MCP protocol:
# Build all examples
meson compile -C builddir examples
# Run specific examples
./builddir/examples/minimal-server
./builddir/examples/filesystem-server
./builddir/examples/calculator-server
./builddir/examples/chat-server
// Create server information
var server_info = new Mcp.Types.Protocol.ServerInfo (
"my-server",
"1.0.0",
"Description of my MCP server",
"https://my-server.example.com" // Optional website URL
);
// Configure capabilities
var capabilities = new Mcp.Types.Protocol.ServerCapabilities ();
capabilities.logging = true;
capabilities.completions = new Mcp.Types.Protocol.CompletionsCapabilities ();
capabilities.resources = new Mcp.Types.Protocol.ResourcesCapabilities ();
capabilities.tools = new Mcp.Types.Protocol.ToolsCapabilities ();
capabilities.prompts = new Mcp.Types.Protocol.PromptsCapabilities ();
// Create server
var server = new Mcp.Core.Server (server_info, capabilities);
The library provides a comprehensive error domain with proper JSON-RPC error codes:
try {
var result = yield server.tool_manager.call_tool ("calculator", arguments);
// Handle result
} catch (Mcp.Core.Error e) {
// Handle MCP-specific errors
stderr.printf ("MCP Error: %s (code: %d)\n", e.message, e.code);
} catch (Error e) {
// Handle other errors
stderr.printf ("Error: %s\n", e.message);
}
All operations that involve I/O are asynchronous:
// List resources
var resource_list = yield server.resource_manager.list_resources ();
// Call a tool
var tool_result = yield server.tool_manager.call_tool ("my_tool", arguments);
// Get a prompt
var prompt = yield server.prompt_manager.get_prompt ("my_prompt", arguments);
The implementation follows these key design principles:
mcp-vala/
├── src/
│ ├── core/
│ │ ├── compat.vala # Compatibility utilities (InjectContentLength, StripContentLength)
│ │ ├── error.vala # Error domain and utilities
│ │ └── server.vala # Main server class using Jsonrpc.Server
│ ├── types/
│ │ ├── common.vala # Common content types
│ │ ├── protocol.vala # Protocol types (ServerInfo, Capabilities)
│ │ └── variant_utils.vala # Variant utility functions
│ ├── resources/
│ │ ├── manager.vala # Resource manager
│ │ ├── provider.vala # Resource provider interface and base class
│ │ └── types.vala # Resource data types
│ ├── tools/
│ │ ├── executor.vala # Tool executor interface and base class
│ │ ├── manager.vala # Tool manager
│ │ └── types.vala # Tool data types
│ ├── prompts/
│ │ ├── manager.vala # Prompt manager
│ │ ├── template.vala # Prompt template interface and base class
│ │ └── types.vala # Prompt data types
│ └── meson.build # Library build configuration
├── examples/
│ ├── minimal-server.vala # Minimal server example
│ ├── simple-server.vala # Simple server with resources
│ ├── filesystem-server.vala # File system resource example
│ ├── calculator-server.vala # Tool execution example
│ ├── chat-server.vala # Prompt handling example
│ └── meson.build # Examples build configuration
├── docs/
│ ├── api-reference.md # API documentation
│ ├── developer-guide.md # Developer guide
│ └── meson.build # Documentation build configuration
├── meson.build # Main build configuration
├── .gitignore # Git ignore file
└── README.md # This file
This library is licensed under the GNU Lesser General Public License v2.1 or later. See individual source files for specific license information.
Contributions are welcome! Please ensure:
The core MCP server functionality is implemented and ready for use. The library now uses jsonrpc-glib-1.0 for standard-compliant JSON-RPC 2.0 communication, providing improved IO reliability and error handling. This provides a solid foundation for building MCP servers in Vala with proper protocol compliance and extensibility.