2026-10-10 05:30:48 +02:00
//! `eureka-state`: Huginn's mind as six MCP tools over stdio.
2026-09-30 00:36:33 +02:00
//!
2026-10-10 05:30:48 +02:00
//! One tool per wire operation (`whoami`, `admit`, `view`, `query`,
//! `persona_get`, `persona_put`), each a `HuginnClient` call and nothing else. The declared identity is this
2026-09-30 00:36:33 +02:00
//! process's configuration, not a per-call argument: `EUREKA_INSTANCE` names
//! the mind and `HUGINN_ENDPOINT` (`rudp://host:port`) the daemon that holds
//! it. The server fills in `instance` and `provenance.tool`, so a caller can
//! name neither.
//!
//! Bad configuration does not stop the server: an MCP server that dies at
//! startup vanishes from Claude Code without a word. `whoami` reports it and
//! every other tool refuses with it. A refusal from the mind is an answer and
//! comes back as a normal result; a daemon that cannot be reached or that
eureka-state: typed TooLarge, typed InvalidInput, and the pins Soul found missing
- The client refuses a request whose encoded size exceeds what one RUDP send
carries (65,507 less the 36-byte header and the 6-byte channel id), before
connecting, as ClientError::TooLarge { bytes, limit }. It was reported as
a retryable Unavailable ("Message too long"). The MCP tools surface it as
isError with {"error":"TooLarge","bytes","limit"}.
- Arguments that fail to deserialize are still a tool error, now with the
same typed body as every other one: {"error":"InvalidInput","detail"}.
- A non-UTF-8 EUREKA_INSTANCE or HUGINN_ENDPOINT is reported as not valid
UTF-8 and not as unset.
- Tests: the schema walk asserts no $id or $schema below any tool's root; the
15 s timeout is measured against a silent daemon; the semantic part of a
query is observed on the wire; Internal's and TooLarge's bodies and the
InvalidInput rewrite are pinned; attribution is refused through the MCP
admit; oversize is TooLarge at and one past the limit.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-09-30 02:51:50 +02:00
//! rejects the envelope, or a request too large for one send, is a result with
//! `isError: true`, typed, so a caller can tell a down daemon from a permanent
//! mismatch. Arguments that do not fit a tool's input are typed the same way
//! (`InvalidInput`), as a tool error and not a protocol one.
2026-09-30 00:36:33 +02:00
use std ::net ::SocketAddr ;
use std ::sync ::Arc ;
use std ::time ::Duration ;
use cultnet_rs ::Selection ;
2026-11-04 22:06:41 +01:00
use eureka_state ::door ::{ self , Refusal } ;
2026-09-30 00:36:33 +02:00
use eureka_state ::{ ClientError , HuginnClient } ;
2026-10-09 14:09:16 +02:00
use huginn_mind ::eureka_pipeline ::{ PipelineDocument , PipelineRef , Short , Slug } ;
2026-09-30 00:36:33 +02:00
use huginn_mind ::{
Faculty , HuginnMindRequest , HuginnMindResponse , PipelineAdmissionBatch , PipelineProvenance , SemanticQuery ,
} ;
use rmcp ::handler ::server ::router ::tool ::ToolRouter ;
eureka-state: typed TooLarge, typed InvalidInput, and the pins Soul found missing
- The client refuses a request whose encoded size exceeds what one RUDP send
carries (65,507 less the 36-byte header and the 6-byte channel id), before
connecting, as ClientError::TooLarge { bytes, limit }. It was reported as
a retryable Unavailable ("Message too long"). The MCP tools surface it as
isError with {"error":"TooLarge","bytes","limit"}.
- Arguments that fail to deserialize are still a tool error, now with the
same typed body as every other one: {"error":"InvalidInput","detail"}.
- A non-UTF-8 EUREKA_INSTANCE or HUGINN_ENDPOINT is reported as not valid
UTF-8 and not as unset.
- Tests: the schema walk asserts no $id or $schema below any tool's root; the
15 s timeout is measured against a silent daemon; the semantic part of a
query is observed on the wire; Internal's and TooLarge's bodies and the
InvalidInput rewrite are pinned; attribution is refused through the MCP
admit; oversize is TooLarge at and one past the limit.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-09-30 02:51:50 +02:00
use rmcp ::handler ::server ::tool ::ToolCallContext ;
2026-09-30 00:36:33 +02:00
use rmcp ::handler ::server ::wrapper ::Parameters ;
eureka-state: typed TooLarge, typed InvalidInput, and the pins Soul found missing
- The client refuses a request whose encoded size exceeds what one RUDP send
carries (65,507 less the 36-byte header and the 6-byte channel id), before
connecting, as ClientError::TooLarge { bytes, limit }. It was reported as
a retryable Unavailable ("Message too long"). The MCP tools surface it as
isError with {"error":"TooLarge","bytes","limit"}.
- Arguments that fail to deserialize are still a tool error, now with the
same typed body as every other one: {"error":"InvalidInput","detail"}.
- A non-UTF-8 EUREKA_INSTANCE or HUGINN_ENDPOINT is reported as not valid
UTF-8 and not as unset.
- Tests: the schema walk asserts no $id or $schema below any tool's root; the
15 s timeout is measured against a silent daemon; the semantic part of a
query is observed on the wire; Internal's and TooLarge's bodies and the
InvalidInput rewrite are pinned; attribution is refused through the MCP
admit; oversize is TooLarge at and one past the limit.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-09-30 02:51:50 +02:00
use rmcp ::model ::{ CallToolRequestParams , CallToolResult , ContentBlock , ServerCapabilities , ServerInfo } ;
use rmcp ::service ::RequestContext ;
use rmcp ::{ ErrorData , RoleServer , ServerHandler , ServiceExt , tool , tool_handler , tool_router } ;
2026-09-30 00:36:33 +02:00
use schemars ::{ JsonSchema , Schema , SchemaGenerator } ;
use serde ::Deserialize ;
use serde_json ::{ Value , json } ;
/// The `provenance.tool` every admission from this server carries.
const TOOL : & str = " eureka-state " ;
/// How long one call may take, connection to last byte.
const TIMEOUT : Duration = Duration ::from_secs ( 15 ) ;
/// CultLib's published shape of a selection, vendored because the selection
/// type carries no schema of its own. `selection_schema_matches_cultlib` pins
/// it to the CultLib revision `cultnet-rs` is built from.
const SELECTION_SCHEMA : & str = include_str! ( " ../schemas/cultnet.selection.schema.json " ) ;
/// The selection's schema, self-contained: its own `$defs` move into the
/// tool's, so every `$ref` in the input schema is `#/$defs/...`.
fn selection_schema ( generator : & mut SchemaGenerator ) -> Schema {
let Ok ( Value ::Object ( mut selection ) ) = serde_json ::from_str ( SELECTION_SCHEMA ) else {
unreachable! ( " the vendored selection schema is a JSON object " ) ;
} ;
selection . remove ( " $schema " ) ;
selection . remove ( " $id " ) ;
if let Some ( Value ::Object ( defs ) ) = selection . remove ( " $defs " ) {
generator . definitions_mut ( ) . extend ( defs ) ;
}
Schema ::try_from ( Value ::Object ( selection ) ) . expect ( " an object is a schema " )
}
#[ derive(Deserialize, JsonSchema) ]
struct AdmitInput {
/// The faculty the batch is admitted as. Attribution, not authority.
faculty : Faculty ,
/// The admitting agent's name.
agent : Short ,
/// The admitting session, recorded on the receipt so that several sessions
/// sharing one mind stay tellable apart.
session : Short ,
/// The documents to admit as one batch. The mind admits them whole or refuses them.
2026-11-04 22:06:41 +01:00
/// Decoded by the text door, which refuses a key the schema does not know.
#[ schemars(with = " Vec<PipelineDocument> " ) ]
documents : Value ,
2026-09-30 00:36:33 +02:00
}
2026-10-10 05:30:48 +02:00
#[ derive(Deserialize, JsonSchema) ]
struct PersonaPutInput {
/// The faculty the put is attributed to. Attribution, not authority.
faculty : Faculty ,
/// The putting agent's name.
agent : Short ,
/// The putting session, recorded on the receipt.
session : Short ,
/// The whole `gamecult.persona_state.v0` document. It replaces the stored
/// one; there is no partial write.
state : Value ,
/// The stored document's `updatedAt`; omit it for the first put.
expected_updated_at : Option < String > ,
}
2026-09-30 00:36:33 +02:00
#[ derive(Deserialize, JsonSchema) ]
struct ViewInput {
/// The document's id, as a query or an admission returned it.
id : PipelineRef ,
}
#[ derive(Deserialize, JsonSchema) ]
struct QueryInput {
/// A CultNet typed selection over the mind's documents.
#[ schemars(schema_with = " selection_schema " ) ]
selection : Selection ,
/// Optional: rank by nearness to this text instead of ordering by key.
semantic : Option < SemanticQuery > ,
}
/// Why a call produced no answer. Each is a typed body with `isError: true`.
enum Trouble {
Misconfigured ( String ) ,
Client ( ClientError ) ,
Internal ( String ) ,
2026-11-04 22:06:41 +01:00
/// Input the schema does not describe, refused by the text door before any call.
Invalid ( String ) ,
2026-09-30 00:36:33 +02:00
}
impl Trouble {
fn body ( & self ) -> Value {
match self {
Self ::Misconfigured ( detail ) = > json! ( { " error " : " Misconfigured " , " detail " : detail } ) ,
Self ::Client ( ClientError ::Unavailable { endpoint , detail } ) = > {
json! ( { " error " : " Unavailable " , " endpoint " : format ! ( " rudp://{endpoint} " ) , " detail " : detail } )
}
Self ::Client ( ClientError ::Rejected { endpoint , code , detail } ) = > {
json! ( { " error " : " Rejected " , " endpoint " : format ! ( " rudp://{endpoint} " ) , " code " : code , " detail " : detail } )
}
eureka-state: typed TooLarge, typed InvalidInput, and the pins Soul found missing
- The client refuses a request whose encoded size exceeds what one RUDP send
carries (65,507 less the 36-byte header and the 6-byte channel id), before
connecting, as ClientError::TooLarge { bytes, limit }. It was reported as
a retryable Unavailable ("Message too long"). The MCP tools surface it as
isError with {"error":"TooLarge","bytes","limit"}.
- Arguments that fail to deserialize are still a tool error, now with the
same typed body as every other one: {"error":"InvalidInput","detail"}.
- A non-UTF-8 EUREKA_INSTANCE or HUGINN_ENDPOINT is reported as not valid
UTF-8 and not as unset.
- Tests: the schema walk asserts no $id or $schema below any tool's root; the
15 s timeout is measured against a silent daemon; the semantic part of a
query is observed on the wire; Internal's and TooLarge's bodies and the
InvalidInput rewrite are pinned; attribution is refused through the MCP
admit; oversize is TooLarge at and one past the limit.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-09-30 02:51:50 +02:00
Self ::Client ( ClientError ::TooLarge { bytes , limit } ) = > {
json! ( { " error " : " TooLarge " , " bytes " : bytes , " limit " : limit } )
}
2026-09-30 04:55:05 +02:00
Self ::Client ( ClientError ::Unencodable { detail } ) = > json! ( { " error " : " Unencodable " , " detail " : detail } ) ,
2026-09-30 00:36:33 +02:00
Self ::Internal ( detail ) = > json! ( { " error " : " Internal " , " detail " : detail } ) ,
2026-11-04 22:06:41 +01:00
Self ::Invalid ( detail ) = > json! ( { " error " : " InvalidInput " , " detail " : detail } ) ,
2026-09-30 00:36:33 +02:00
}
}
}
/// The two environment values as given, and the client they make when both are
/// valid.
struct Settings {
instance : Option < String > ,
endpoint : Option < String > ,
client : Result < HuginnClient , String > ,
}
impl Settings {
fn from_env ( ) -> Self {
eureka-state: typed TooLarge, typed InvalidInput, and the pins Soul found missing
- The client refuses a request whose encoded size exceeds what one RUDP send
carries (65,507 less the 36-byte header and the 6-byte channel id), before
connecting, as ClientError::TooLarge { bytes, limit }. It was reported as
a retryable Unavailable ("Message too long"). The MCP tools surface it as
isError with {"error":"TooLarge","bytes","limit"}.
- Arguments that fail to deserialize are still a tool error, now with the
same typed body as every other one: {"error":"InvalidInput","detail"}.
- A non-UTF-8 EUREKA_INSTANCE or HUGINN_ENDPOINT is reported as not valid
UTF-8 and not as unset.
- Tests: the schema walk asserts no $id or $schema below any tool's root; the
15 s timeout is measured against a silent daemon; the semantic part of a
query is observed on the wire; Internal's and TooLarge's bodies and the
InvalidInput rewrite are pinned; attribution is refused through the MCP
admit; oversize is TooLarge at and one past the limit.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-09-30 02:51:50 +02:00
let instance = variable ( " EUREKA_INSTANCE " ) ;
let endpoint = variable ( " HUGINN_ENDPOINT " ) ;
let shown = | value : & Result < Option < String > , String > | value . as_ref ( ) . ok ( ) . cloned ( ) . flatten ( ) ;
let ( shown_instance , shown_endpoint ) = ( shown ( & instance ) , shown ( & endpoint ) ) ;
Self { instance : shown_instance , endpoint : shown_endpoint , client : client ( instance , endpoint ) }
}
}
/// An environment variable: its value, `None` when unset, or why it cannot be read.
fn variable ( name : & str ) -> Result < Option < String > , String > {
match std ::env ::var ( name ) {
Ok ( value ) = > Ok ( Some ( value ) ) ,
Err ( std ::env ::VarError ::NotPresent ) = > Ok ( None ) ,
Err ( std ::env ::VarError ::NotUnicode ( _ ) ) = > Err ( format! ( " {name} is not valid UTF-8 " ) ) ,
2026-09-30 00:36:33 +02:00
}
}
eureka-state: typed TooLarge, typed InvalidInput, and the pins Soul found missing
- The client refuses a request whose encoded size exceeds what one RUDP send
carries (65,507 less the 36-byte header and the 6-byte channel id), before
connecting, as ClientError::TooLarge { bytes, limit }. It was reported as
a retryable Unavailable ("Message too long"). The MCP tools surface it as
isError with {"error":"TooLarge","bytes","limit"}.
- Arguments that fail to deserialize are still a tool error, now with the
same typed body as every other one: {"error":"InvalidInput","detail"}.
- A non-UTF-8 EUREKA_INSTANCE or HUGINN_ENDPOINT is reported as not valid
UTF-8 and not as unset.
- Tests: the schema walk asserts no $id or $schema below any tool's root; the
15 s timeout is measured against a silent daemon; the semantic part of a
query is observed on the wire; Internal's and TooLarge's bodies and the
InvalidInput rewrite are pinned; attribution is refused through the MCP
admit; oversize is TooLarge at and one past the limit.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-09-30 02:51:50 +02:00
fn client ( instance : Result < Option < String > , String > , endpoint : Result < Option < String > , String > ) -> Result < HuginnClient , String > {
let instance = instance ? . ok_or ( " EUREKA_INSTANCE is not set " ) ? ;
let slug = Slug ( instance ) ;
2026-09-30 00:36:33 +02:00
slug . validate_slug ( ) . map_err ( | refusal | format! ( " EUREKA_INSTANCE is not a valid instance name: {refusal:?} " ) ) ? ;
eureka-state: typed TooLarge, typed InvalidInput, and the pins Soul found missing
- The client refuses a request whose encoded size exceeds what one RUDP send
carries (65,507 less the 36-byte header and the 6-byte channel id), before
connecting, as ClientError::TooLarge { bytes, limit }. It was reported as
a retryable Unavailable ("Message too long"). The MCP tools surface it as
isError with {"error":"TooLarge","bytes","limit"}.
- Arguments that fail to deserialize are still a tool error, now with the
same typed body as every other one: {"error":"InvalidInput","detail"}.
- A non-UTF-8 EUREKA_INSTANCE or HUGINN_ENDPOINT is reported as not valid
UTF-8 and not as unset.
- Tests: the schema walk asserts no $id or $schema below any tool's root; the
15 s timeout is measured against a silent daemon; the semantic part of a
query is observed on the wire; Internal's and TooLarge's bodies and the
InvalidInput rewrite are pinned; attribution is refused through the MCP
admit; oversize is TooLarge at and one past the limit.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-09-30 02:51:50 +02:00
let endpoint = endpoint ? . ok_or ( " HUGINN_ENDPOINT is not set " ) ? ;
2026-09-30 00:36:33 +02:00
let address = endpoint
. strip_prefix ( " rudp:// " )
. and_then ( | address | address . parse ::< SocketAddr > ( ) . ok ( ) )
. ok_or_else ( | | format! ( " HUGINN_ENDPOINT is not rudp://<ip>:<port>: {endpoint} " ) ) ? ;
Ok ( HuginnClient ::new ( address , slug , TIMEOUT ) )
}
#[ derive(Clone) ]
struct EurekaState {
settings : Arc < Settings > ,
tool_router : ToolRouter < Self > ,
}
impl EurekaState {
fn new ( settings : Settings ) -> Self {
Self { settings : Arc ::new ( settings ) , tool_router : Self ::tool_router ( ) }
}
/// One wire call for the configured instance. The request is built from
/// that instance, never from anything the caller sent.
2026-10-10 05:30:48 +02:00
async fn call (
& self ,
request : impl FnOnce ( Slug ) -> HuginnMindRequest + Send + 'static ,
) -> Result < HuginnMindResponse , Trouble > {
self . ask ( move | client | client . call ( request ( client . instance ( ) . clone ( ) ) ) ) . await
}
/// One `HuginnClient` call for the configured instance, off the async
/// thread.
async fn ask (
& self ,
call : impl FnOnce ( & HuginnClient ) -> Result < HuginnMindResponse , ClientError > + Send + 'static ,
) -> Result < HuginnMindResponse , Trouble > {
2026-09-30 00:36:33 +02:00
let client = self . settings . client . as_ref ( ) . map_err ( | detail | Trouble ::Misconfigured ( detail . clone ( ) ) ) ? . clone ( ) ;
2026-10-10 05:30:48 +02:00
tokio ::task ::spawn_blocking ( move | | call ( & client ) )
2026-09-30 00:36:33 +02:00
. await
. map_err ( | error | Trouble ::Internal ( error . to_string ( ) ) ) ?
. map_err ( Trouble ::Client )
}
2026-10-10 05:30:48 +02:00
async fn answer ( & self , request : impl FnOnce ( Slug ) -> HuginnMindRequest + Send + 'static ) -> CallToolResult {
Self ::replied ( self . call ( request ) . await )
}
fn replied ( result : Result < HuginnMindResponse , Trouble > ) -> CallToolResult {
match result {
2026-09-30 00:36:33 +02:00
Ok ( response ) = > reply ( payload ( response ) , false ) ,
Err ( trouble ) = > reply ( trouble . body ( ) , true ) ,
}
}
}
/// The answer as the tool's output: the operation's own payload, or the typed
/// refusal, tagged, when the mind refused the request.
fn payload ( response : HuginnMindResponse ) -> Value {
let value = match response {
HuginnMindResponse ::Whoami ( status ) = > serde_json ::to_value ( status ) ,
HuginnMindResponse ::Admit ( outcome ) = > serde_json ::to_value ( outcome ) ,
HuginnMindResponse ::View ( view ) = > serde_json ::to_value ( view ) ,
HuginnMindResponse ::Query ( page ) = > serde_json ::to_value ( page ) ,
2026-10-10 05:30:48 +02:00
HuginnMindResponse ::Persona ( view ) = > serde_json ::to_value ( view ) ,
HuginnMindResponse ::PersonaPut ( outcome ) = > serde_json ::to_value ( outcome ) ,
2026-09-30 00:36:33 +02:00
other = > serde_json ::to_value ( other ) ,
} ;
value . expect ( " wire types serialize " )
}
fn reply ( body : Value , is_error : bool ) -> CallToolResult {
let content = vec! [ ContentBlock ::text ( body . to_string ( ) ) ] ;
if is_error { CallToolResult ::error ( content ) } else { CallToolResult ::success ( content ) }
}
#[ tool_router ]
impl EurekaState {
#[ tool(
description = " Who this server acts for and whether the Huginn daemon answers: the configured instance and endpoint, reachable, and the mind's status when it does. Reports bad configuration instead of failing. "
) ]
async fn whoami ( & self ) -> CallToolResult {
let mut out = json! ( {
" instance " : self . settings . instance ,
" endpoint " : self . settings . endpoint ,
" reachable " : false ,
" status " : null ,
} ) ;
match self . call ( | _ | HuginnMindRequest ::Whoami ) . await {
Ok ( HuginnMindResponse ::Whoami ( status ) ) = > {
out [ " reachable " ] = json! ( true ) ;
out [ " status " ] = serde_json ::to_value ( status ) . expect ( " wire types serialize " ) ;
}
Ok ( other ) = > out [ " error " ] = json! ( { " error " : " Internal " , " detail " : format ! ( " unexpected answer: {other:?} " ) } ) ,
Err ( trouble ) = > out [ " error " ] = trouble . body ( ) ,
}
reply ( out , false )
}
#[ tool(
description = " Admit one batch of pipeline documents into the mind, whole or not at all. Returns the outcome: committed with a receipt, already admitted, conflict, or a typed refusal. The instance is this server's configuration; do not pass one. "
) ]
async fn admit ( & self , Parameters ( input ) : Parameters < AdmitInput > ) -> CallToolResult {
2026-11-04 22:06:41 +01:00
let documents = match door ::documents ( & input . documents . to_string ( ) ) {
Ok ( documents ) = > documents ,
Err ( Refusal ::Invalid ( detail ) ) = > return Self ::replied ( Err ( Trouble ::Invalid ( detail ) ) ) ,
Err ( Refusal ::Internal ( detail ) ) = > return Self ::replied ( Err ( Trouble ::Internal ( detail ) ) ) ,
} ;
2026-10-10 05:47:00 +02:00
self . answer ( move | instance | {
2026-09-30 00:36:33 +02:00
HuginnMindRequest ::Admit ( PipelineAdmissionBatch {
instance ,
provenance : PipelineProvenance {
faculty : input . faculty ,
agent : input . agent ,
session : input . session ,
tool : Short ( TOOL . into ( ) ) ,
2026-11-08 17:47:43 +01:00
imported_from : None ,
2026-09-30 00:36:33 +02:00
} ,
2026-11-04 22:06:41 +01:00
documents ,
2026-09-30 00:36:33 +02:00
} )
} )
. await
}
#[ tool(description = " Read one document by id, with its admission facts and status. null when the mind has no such document. " ) ]
async fn view ( & self , Parameters ( input ) : Parameters < ViewInput > ) -> CallToolResult {
2026-10-10 05:47:00 +02:00
self . answer ( move | instance | HuginnMindRequest ::View { instance , id : input . id } ) . await
2026-09-30 00:36:33 +02:00
}
#[ tool(
2026-10-25 19:51:29 +01:00
description = " Query the mind with a CultNet typed selection, optionally ranked by semantic nearness. Returns one page; pass its cursor back in the selection for the next. A semantic answer returns at most top_k members of the selection, nearest first, with scores (one cosine similarity per row, in order). "
2026-09-30 00:36:33 +02:00
) ]
async fn query ( & self , Parameters ( input ) : Parameters < QueryInput > ) -> CallToolResult {
2026-10-10 05:47:00 +02:00
self . answer ( move | instance | HuginnMindRequest ::Query {
2026-09-30 00:36:33 +02:00
instance ,
selection : input . selection ,
semantic : input . semantic ,
} )
. await
}
2026-10-10 05:30:48 +02:00
#[ tool(
description = " The mind's persona document (gamecult.persona_state.v0) with its updatedAt and the receipt of the put that wrote it. null until the first put. "
) ]
async fn persona_get ( & self ) -> CallToolResult {
Self ::replied ( self . ask ( HuginnClient ::persona_get ) . await )
}
#[ tool(
description = " Replace the mind's persona document whole. The document must pass the published schema, name this instance as personaId, carry provenance.authority canonical, and have an updatedAt later than the stored one; expected_updated_at must be the stored updatedAt (omit it for the first put). A refusal is an answer; a stale put names the stored updatedAt to retry against. "
) ]
async fn persona_put ( & self , Parameters ( input ) : Parameters < PersonaPutInput > ) -> CallToolResult {
let provenance = PipelineProvenance {
faculty : input . faculty ,
agent : input . agent ,
session : input . session ,
tool : Short ( TOOL . into ( ) ) ,
2026-11-08 17:47:43 +01:00
imported_from : None ,
2026-10-10 05:30:48 +02:00
} ;
Self ::replied (
self . ask ( move | client | client . persona_put ( provenance , input . state , input . expected_updated_at ) ) . await ,
)
}
2026-09-30 00:36:33 +02:00
}
eureka-state: typed TooLarge, typed InvalidInput, and the pins Soul found missing
- The client refuses a request whose encoded size exceeds what one RUDP send
carries (65,507 less the 36-byte header and the 6-byte channel id), before
connecting, as ClientError::TooLarge { bytes, limit }. It was reported as
a retryable Unavailable ("Message too long"). The MCP tools surface it as
isError with {"error":"TooLarge","bytes","limit"}.
- Arguments that fail to deserialize are still a tool error, now with the
same typed body as every other one: {"error":"InvalidInput","detail"}.
- A non-UTF-8 EUREKA_INSTANCE or HUGINN_ENDPOINT is reported as not valid
UTF-8 and not as unset.
- Tests: the schema walk asserts no $id or $schema below any tool's root; the
15 s timeout is measured against a silent daemon; the semantic part of a
query is observed on the wire; Internal's and TooLarge's bodies and the
InvalidInput rewrite are pinned; attribution is refused through the MCP
admit; oversize is TooLarge at and one past the limit.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-09-30 02:51:50 +02:00
/// Every tool error is a typed JSON body. The ones this server writes already
/// are; the one rmcp writes, for arguments that do not deserialize into the
/// tool's input, is plain text, so it is put into the same shape.
fn typed_error ( result : CallToolResult ) -> CallToolResult {
if result . is_error ! = Some ( true ) {
return result ;
}
let Some ( text ) = result . content . first ( ) . and_then ( ContentBlock ::as_text ) . map ( | text | text . text . as_str ( ) ) else {
return result ;
} ;
if serde_json ::from_str ::< Value > ( text ) . is_ok_and ( | body | body . is_object ( ) ) {
return result ;
}
reply ( json! ( { " error " : " InvalidInput " , " detail " : text } ) , true )
}
2026-09-30 00:36:33 +02:00
#[ tool_handler ]
impl ServerHandler for EurekaState {
eureka-state: typed TooLarge, typed InvalidInput, and the pins Soul found missing
- The client refuses a request whose encoded size exceeds what one RUDP send
carries (65,507 less the 36-byte header and the 6-byte channel id), before
connecting, as ClientError::TooLarge { bytes, limit }. It was reported as
a retryable Unavailable ("Message too long"). The MCP tools surface it as
isError with {"error":"TooLarge","bytes","limit"}.
- Arguments that fail to deserialize are still a tool error, now with the
same typed body as every other one: {"error":"InvalidInput","detail"}.
- A non-UTF-8 EUREKA_INSTANCE or HUGINN_ENDPOINT is reported as not valid
UTF-8 and not as unset.
- Tests: the schema walk asserts no $id or $schema below any tool's root; the
15 s timeout is measured against a silent daemon; the semantic part of a
query is observed on the wire; Internal's and TooLarge's bodies and the
InvalidInput rewrite are pinned; attribution is refused through the MCP
admit; oversize is TooLarge at and one past the limit.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-09-30 02:51:50 +02:00
async fn call_tool (
& self ,
request : CallToolRequestParams ,
context : RequestContext < RoleServer > ,
) -> Result < CallToolResult , ErrorData > {
Ok ( typed_error ( self . tool_router . call ( ToolCallContext ::new ( self , request , context ) ) . await ? ) )
}
2026-09-30 00:36:33 +02:00
fn get_info ( & self ) -> ServerInfo {
ServerInfo ::new ( ServerCapabilities ::builder ( ) . enable_tools ( ) . build ( ) ) . with_instructions (
2026-10-10 05:30:48 +02:00
" Huginn's typed mind for this instance: whoami, admit, view, query, persona_get, persona_put. Refusals are answers; isError means the daemon could not be reached or rejected the request. " ,
2026-09-30 00:36:33 +02:00
)
}
}
#[ tokio::main(flavor = " current_thread " ) ]
async fn main ( ) -> anyhow ::Result < ( ) > {
let server = EurekaState ::new ( Settings ::from_env ( ) ) . serve ( rmcp ::transport ::stdio ( ) ) . await ? ;
server . waiting ( ) . await ? ;
Ok ( ( ) )
}
eureka-state: typed TooLarge, typed InvalidInput, and the pins Soul found missing
- The client refuses a request whose encoded size exceeds what one RUDP send
carries (65,507 less the 36-byte header and the 6-byte channel id), before
connecting, as ClientError::TooLarge { bytes, limit }. It was reported as
a retryable Unavailable ("Message too long"). The MCP tools surface it as
isError with {"error":"TooLarge","bytes","limit"}.
- Arguments that fail to deserialize are still a tool error, now with the
same typed body as every other one: {"error":"InvalidInput","detail"}.
- A non-UTF-8 EUREKA_INSTANCE or HUGINN_ENDPOINT is reported as not valid
UTF-8 and not as unset.
- Tests: the schema walk asserts no $id or $schema below any tool's root; the
15 s timeout is measured against a silent daemon; the semantic part of a
query is observed on the wire; Internal's and TooLarge's bodies and the
InvalidInput rewrite are pinned; attribution is refused through the MCP
admit; oversize is TooLarge at and one past the limit.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-09-30 02:51:50 +02:00
#[ cfg(test) ]
mod tests {
use super ::* ;
fn body_of ( result : & CallToolResult ) -> ( bool , String ) {
( result . is_error = = Some ( true ) , result . content [ 0 ] . as_text ( ) . unwrap ( ) . text . clone ( ) )
}
/// Reaching `Internal` for real takes a panic in a blocking task, so its
/// body is pinned where it is built.
#[ test ]
fn every_trouble_has_its_typed_body ( ) {
assert_eq! ( Trouble ::Internal ( " task panicked " . into ( ) ) . body ( ) , json! ( { " error " : " Internal " , " detail " : " task panicked " } ) ) ;
assert_eq! (
Trouble ::Client ( ClientError ::TooLarge { bytes : 70_000 , limit : 65_000 } ) . body ( ) ,
json! ( { " error " : " TooLarge " , " bytes " : 70_000 , " limit " : 65_000 } )
) ;
2026-09-30 04:55:05 +02:00
assert_eq! (
Trouble ::Client ( ClientError ::Unencodable { detail : " no " . into ( ) } ) . body ( ) ,
json! ( { " error " : " Unencodable " , " detail " : " no " } )
) ;
eureka-state: typed TooLarge, typed InvalidInput, and the pins Soul found missing
- The client refuses a request whose encoded size exceeds what one RUDP send
carries (65,507 less the 36-byte header and the 6-byte channel id), before
connecting, as ClientError::TooLarge { bytes, limit }. It was reported as
a retryable Unavailable ("Message too long"). The MCP tools surface it as
isError with {"error":"TooLarge","bytes","limit"}.
- Arguments that fail to deserialize are still a tool error, now with the
same typed body as every other one: {"error":"InvalidInput","detail"}.
- A non-UTF-8 EUREKA_INSTANCE or HUGINN_ENDPOINT is reported as not valid
UTF-8 and not as unset.
- Tests: the schema walk asserts no $id or $schema below any tool's root; the
15 s timeout is measured against a silent daemon; the semantic part of a
query is observed on the wire; Internal's and TooLarge's bodies and the
InvalidInput rewrite are pinned; attribution is refused through the MCP
admit; oversize is TooLarge at and one past the limit.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-09-30 02:51:50 +02:00
assert_eq! ( Trouble ::Misconfigured ( " bad " . into ( ) ) . body ( ) , json! ( { " error " : " Misconfigured " , " detail " : " bad " } ) ) ;
}
/// Only the text rmcp writes is rewritten: our own typed bodies and every
/// success pass through untouched.
#[ test ]
fn typed_error_rewrites_only_rmcp_plain_text ( ) {
let plain = CallToolResult ::error ( vec! [ ContentBlock ::text ( " failed to deserialize parameters: missing field `faculty` " ) ] ) ;
let ( is_error , text ) = body_of ( & typed_error ( plain ) ) ;
assert! ( is_error ) ;
assert_eq! (
serde_json ::from_str ::< Value > ( & text ) . unwrap ( ) ,
json! ( { " error " : " InvalidInput " , " detail " : " failed to deserialize parameters: missing field `faculty` " } )
) ;
let typed = reply ( json! ( { " error " : " Unavailable " , " detail " : " down " } ) , true ) ;
assert_eq! ( body_of ( & typed_error ( typed . clone ( ) ) ) , body_of ( & typed ) ) ;
let success = CallToolResult ::success ( vec! [ ContentBlock ::text ( " not json, and fine " ) ] ) ;
assert_eq! ( body_of ( & typed_error ( success ) ) , ( false , " not json, and fine " . to_string ( ) ) ) ;
let quoted = CallToolResult ::error ( vec! [ ContentBlock ::text ( " [1, 2] " ) ] ) ;
assert_eq! ( serde_json ::from_str ::< Value > ( & body_of ( & typed_error ( quoted ) ) . 1 ) . unwrap ( ) [ " error " ] , json! ( " InvalidInput " ) ) ;
}
}