pforge-bridge
Language bridges for polyglot MCP handlers - write handlers in Python, Go, Node.js, and other languages while using pforge's Rust runtime.
Features
- Multi-Language Support: Python, Go, Node.js, and more
- Zero-Copy FFI: Efficient data passing across language boundaries
- Type Safety: Preserve type information across language bridges
- Error Propagation: Native error semantics in each language
- Stable ABI: C FFI for maximum compatibility
- Async Support: Works with async handlers in all languages
Supported Languages
| Language | Status | Features |
|---|---|---|
| Python | â Stable | Async/await, type hints, error handling |
| Go | ð§ Beta | Goroutines, error handling |
| Node.js | ð§ Beta | Promises, TypeScript support |
| Ruby | ð Planned | - |
| Java | ð Planned | - |
Installation
Quick Start
Python Handler
pforge.yaml:
tools:
- type: native
name: analyze_data
description: "Analyze data with Python"
handler:
bridge: python
path: handlers.analyze_data
params:
data:
handlers.py:
"""Analyze data using numpy and pandas"""
=
return
Go Handler
pforge.yaml:
tools:
- type: native
name: process_image
description: "Process image with Go"
handler:
bridge: go
path: handlers.ProcessImage
params:
image_url:
handlers.go:
package handlers
import (
"encoding/json"
"image"
_ "image/jpeg"
"net/http"
)
func ProcessImage(params map[string]interface) (interface, error)
Node.js Handler
pforge.yaml:
tools:
- type: native
name: fetch_data
description: "Fetch data with Node.js"
handler:
bridge: node
path: handlers.fetchData
params:
url:
handlers.js:
export
Architecture
C FFI Layer
The bridge uses a stable C ABI for maximum compatibility:
// Bridge function signature
typedef struct BridgeData;
typedef ;
Zero-Copy Parameter Passing
Parameters are passed as pointers, not serialized:
// Rust side
let params_ptr = params.as_ptr;
let params_len = params.len;
// Python side receives raw pointer
let result = python_handler;
Error Handling
Errors propagate with full context:
# Python raises exception
// Rust receives typed error
Err
Bridge Configuration
Python Bridge
bridges:
python:
runtime: cpython # or pypy
version: "3.11"
venv: .venv # Optional virtualenv
requirements: requirements.txt
Install dependencies:
Go Bridge
bridges:
go:
version: "1.21"
mod: handlers # Go module name
Install bridge:
Node.js Bridge
bridges:
node:
runtime: node # or bun, deno
version: "20"
package: package.json
Install bridge:
Advanced Usage
Type Mapping
Automatic type conversion between languages:
| JSON Type | Rust | Python | Go | Node.js |
|---|---|---|---|---|
| string | String | str | string | string |
| number | f64 | float | float64 | number |
| integer | i64 | int | int64 | number |
| boolean | bool | bool | bool | boolean |
| array | Vec | list | []interface{} | Array |
| object | HashMap | dict | map | Object |
Custom Serialization
Override default serialization:
return
State Sharing
Share state between Rust and bridge languages:
// Rust side
let state = new;
bridge.set_state;
# Python side
=
+= 1
Performance
| Operation | Overhead |
|---|---|
| Function call | ~1-5Ξs |
| Parameter passing (1KB) | ~100ns |
| Result marshaling (1KB) | ~100ns |
| Error propagation | ~500ns |
The bridge is optimized for minimal overhead while maintaining safety.
Security
- Sandboxing: Optional process isolation for handlers
- Resource Limits: Memory and CPU limits per handler
- Capability-Based: Handlers only access granted capabilities
- Input Validation: Automatic validation of bridge parameters
Debugging
Enable debug logging:
PFORGE_BRIDGE_LOG=debug
Trace bridge calls:
bridge.enable_tracing;
Limitations
- No direct pointer sharing between languages
- Serialization overhead for complex types
- Each bridge adds ~5MB to binary size
- Requires language runtime installed
Documentation
Contributing
We welcome bridge implementations for additional languages! Please open an issue or pull request on GitHub.
License
MIT