Profpatsch/users/Profpatsch/varlink-mcp-bridge
- .gitignore 38 B
- CLAUDE.md 252 B
- README.md 1.5 KiB
- cache.go 6.4 KiB
- default.nix 721 B
- go-deps.nix 101 B
- go.mod 711 B
- go.sum 2.1 KiB
- main.go 18.7 KiB
- translator.go 3.3 KiB
- types.go 1.9 KiB
- varlink-mcp-bridge.1 6.5 KiB
- varlink-mcp-bridge.service 481 B
Varlink MCP Bridge
A bridge that exposes Varlink services as MCP (Model Context Protocol) tools for Claude Code.
Quick Start
Install:
nix profile install https://sources.profpatsch.de/Profpatsch.tar.gz#varlink-mcp-bridge
Configure (.mcp.json in your project):
{
"mcpServers": {
"my-service": {
"command": "varlink-mcp-bridge",
"args": [
"--varlink-address=unix:/run/user/1000/my.service.Socket",
"--interface=my.service.Interface"
]
}
}
}
Restart Claude Code to load the MCP server.
Documentation
See man varlink-mcp-bridge for comprehensive documentation including:
- MCP integration patterns
- Type mapping (Varlink IDL → JSON Schema)
- Error handling
- Examples and troubleshooting
- Implementation details
Or view the manpage directly:
man ./varlink-mcp-bridge.1
What It Does
- Connects to your Varlink service
- Introspects the interface definition
- Exposes each method as an MCP tool with auto-generated JSON Schema
- Translates tool calls ↔ Varlink method invocations
- Preserves documentation from Varlink interface comments
Example: A Varlink method GetLastPosition() -> (path: string, line: int) becomes an MCP tool GetLastPosition that Claude can invoke.
Building
With Nix:
nix-build ~/kot/Profpatsch -A users.Profpatsch.varlink-mcp-bridge
With Go:
go build
varlink-mcp-bridge(1)
NAME
varlink-mcp-bridge - expose Varlink services as MCP (Model Context Protocol) tools
SYNOPSIS
varlink-mcp-bridge --varlink-address address --interface name [--debug]
DESCRIPTION
varlink-mcp-bridge connects to a Varlink service, introspects its interface definition, and exposes all methods as MCP tools over stdio. This enables Claude Code to invoke Varlink service methods directly as AI-available tools with automatic type translation and documentation.
The bridge handles the complete protocol translation:
- Varlink IDL types → JSON Schema (for MCP tool definitions)
- MCP tool calls → Varlink method invocations
- Varlink responses → MCP tool results
The following options are available:
--varlink-address address
Varlink service address to connect to. Supports Unix domain sockets (unix:/path/to/socket) or TCP addresses (tcp:host:port). Required.
--interface name
Varlink interface name to expose via MCP. Must match an interface provided by the service at the given address. The interface name follows reverse-DNS notation (e.g., de.profpatsch.ClaudeLastPosition). Required.
--debug
Enable verbose debug logging to stderr. Shows all JSON-RPC messages, Varlink calls, and internal state transitions. Useful for troubleshooting bridge configuration or service communication issues.
MCP INTEGRATION
varlink-mcp-bridge is designed to be invoked by MCP clients (such as Claude Code) via the stdio transport. It communicates using JSON-RPC 2.0 protocol and follows the MCP specification.
Configuration
Create or edit .mcp.json in your project directory:
{
"mcpServers": {
"my-service": {
"command": "/path/to/varlink-mcp-bridge",
"args": [
"--varlink-address=unix:/run/user/1000/my.service.Socket",
"--interface=my.service.Interface"
]
}
}
}
Multiple Varlink services can be exposed by adding additional entries to mcpServers. Each bridge instance connects to one interface.
Tool Naming
MCP tool names are taken directly from Varlink method names with no transformation. For example:
Interface: de.profpatsch.DisplayBrightness
Method: SetBrightnessAllMonitors
Tool name: SetBrightnessAllMonitors
This keeps tool names consistent with the original Varlink interface and makes debugging easier since tool names match method names exactly
Documentation
Tool descriptions are extracted directly from Varlink interface documentation comments (using # syntax). This makes MCP tools self-documenting and discoverable in Claude Code's tool browser.
HOW IT WORKS
varlink-mcp-bridge performs the following operations on startup:
-
Connects to the Varlink service at the specified address
-
Calls org.varlink.service.GetInterfaceDescription to retrieve the interface definition
-
Parses the Varlink IDL using the official IDL parser
-
Translates each method signature into MCP tool schema with JSON Schema types
-
Enters stdio loop, processing MCP JSON-RPC requests
-
For each tools/call request, invokes the corresponding Varlink method
-
Returns Varlink response data formatted as MCP tool result
Connections to the Varlink service are persistent and reused across multiple tool invocations for efficiency.
TYPE MAPPING
Varlink types are translated to JSON Schema as follows:
bool
boolean
int, float
integer, number
string
string
[]T
array with items type T
?T
Optional type (not in required list)
[string]T
object with additionalProperties of type T
(field: T, ...)
object with typed properties
EXAMPLES
Expose Edit Position Tracker
$ varlink-mcp-bridge \
--varlink-address=unix:/run/user/1000/de.Profpatsch.ClaudeLastPosition \
--interface=de.profpatsch.ClaudeLastPosition
This exposes methods like GetLastPosition and SetLastPosition as MCP tools that Claude can invoke.
Expose Display Brightness Control
$ varlink-mcp-bridge \
--varlink-address=unix:/run/user/1000/de.Profpatsch.DisplayBrightness \
--interface=de.profpatsch.DisplayBrightness
Debug Mode
$ varlink-mcp-bridge \
--varlink-address=unix:/run/user/1000/my.Service \
--interface=my.Service \
--debug 2>debug.log
Logs all protocol messages to debug.log for troubleshooting.
INSTALLATION
Build and install using Nix flakes:
$ nix profile install git+https://codeberg.org/Profpatsch/Profpatsch?ref=canon#varlink-mcp-bridge
This installs varlink-mcp-bridge and the man page to your Nix profile.
ERROR HANDLING
varlink-mcp-bridge returns MCP error responses in the following cases:
Parse Error (-32700)
Invalid JSON received from MCP client
Invalid Request (-32600)
Malformed JSON-RPC request (missing required fields, non-monotonic ID)
Method Not Found (-32601)
Unknown MCP method (not initialize, tools/list, or tools/call)
Invalid Params (-32602)
Unknown tool name or invalid tool arguments
Internal Error (-32603)
Varlink call failed (connection error, unknown method, service error)
Error messages include detailed context to aid debugging, such as the actual Varlink error returned by the service.
FILES
.mcp.json
MCP server configuration file specifying which Varlink services to expose
/run/user/<uid>/*
Common location for user-specific Varlink service Unix sockets
EXIT STATUS
The varlink-mcp-bridge utility exits 0 on success, and >0 if an error occurs.
The bridge runs indefinitely as an MCP server until the client closes stdin or sends a termination signal.
SEE ALSO
varlink(7)
Varlink Protocol: https://varlink.org
Model Context Protocol: https://modelcontextprotocol.io
Claude Code Documentation: https://docs.claude.com/claude-code
AUTHORS
Profpatsch
varlink-mcp-bridge
was created with
Claude Code.
CAVEATS
-
Only one Varlink interface can be exposed per bridge instance. Use multiple .mcp.json entries to expose multiple services.
-
Varlink service must support persistent connections and handle multiple method calls on the same socket.
-
Documentation quality depends on the Varlink interface author including # comment annotations in the interface definition.
-
Complex nested Varlink types may produce verbose JSON Schema that is less readable in tool descriptions.