feat(component2json): add integer-based resource handle serialization#602
feat(component2json): add integer-based resource handle serialization#602JMLX42 wants to merge 1 commit intomicrosoft:mainfrom
Conversation
Implements wassette#601 - WIT resource type support for MCP tool calls. Changes: - Add ResourceHandleTable for mapping integer handles to ResourceAny values - Update val_to_json/json_to_val to accept handle table parameter - Resources serialize to integers (not debug strings) and can be passed back to components via their integer handles - Update schema generation to use "integer" type for resources - Handle tables persist per-component in LifecycleManager, enabling multi-call workflows (open → read → close) This enables wasi:filesystem workflows where file descriptors need to persist across multiple tool calls.
Mossaka
left a comment
There was a problem hiding this comment.
Thanks, this is much needed to get resource handles covered in component2json crate. This is a breaking change.
| pub struct ResourceHandleTable { | ||
| resources: HashMap<u64, ResourceAny>, | ||
| next_handle: u64, | ||
| } |
There was a problem hiding this comment.
perhaps encapsulate this further into a serialize and a deserialize functions so that it doesn't expose internal implementation details like next_handle?
There was a problem hiding this comment.
so that it doesn't expose internal implementation details like next_handle?
I'm not sure I understand the problem:
- Fields are already private - external code cannot access
next_handle ResourceAnyisn't serializable, so the table can't be persisted anyway- We could add custom
serdesupport with opaque serialization if a use case arises
But maybe I'm just misunderstanding the question.
There was a problem hiding this comment.
This is a breaking change.
Is component2json supposed to be a public API? If that's the case, we could keep the original signatures as backward-compatible wrappers, and add new *_with_handles functions:
// Backward-compatible API (resources not preserved across calls)
pub fn vals_to_json(vals: &[Val]) -> Value {
vals_to_json_with_handles(vals, &mut ResourceHandleTable::new())
}
pub fn json_to_vals(value: &Value, types: &[(String, Type)]) -> Result<Vec<Val>, ValError> {
json_to_vals_with_handles(value, types, &mut ResourceHandleTable::new())
}
// New API for resource handling
pub fn vals_to_json_with_handles(vals: &[Val], handles: &mut ResourceHandleTable) -> Value {
// current implementation
}
pub fn json_to_vals_with_handles(
value: &Value,
types: &[(String, Type)],
handles: &mut ResourceHandleTable
) -> Result<Vec<Val>, ValError> {
// current implementation
}
Summary
Implements #601 - WIT resource type support for MCP tool calls.
ResourceHandleTablefor mapping integer handles toResourceAnyvaluesval_to_json/json_to_valto accept handle table parameter"type": "integer"for resourcesLifecycleManager, enabling multi-call workflows (open → read → close)This enables
wasi:filesystemworkflows where file descriptors need to persist across multiple tool calls.Test plan
Disclosure
This PR was developed with assistance from Claude (Anthropic's AI assistant).