httpmcp_rust/
handler_types.rs

1use crate::context::RequestContext;
2use crate::error::Result;
3use crate::protocol::*;
4use actix_multipart::Multipart;
5use actix_web::HttpResponse;
6use futures::future::BoxFuture;
7use serde_json::Value;
8use std::collections::HashMap;
9use std::sync::Arc;
10
11/// Tool handler function signature
12pub type ToolHandler = Box<
13    dyn Fn(HashMap<String, Value>, RequestContext) -> BoxFuture<'static, Result<Value>>
14        + Send
15        + Sync,
16>;
17
18/// Resource list handler function signature
19pub type ResourceListHandler = Box<
20    dyn Fn(
21            Option<String>,
22            RequestContext,
23        ) -> BoxFuture<'static, Result<(Vec<Resource>, Option<String>)>>
24        + Send
25        + Sync,
26>;
27
28/// Resource read handler function signature
29pub type ResourceReadHandler = Box<
30    dyn Fn(String, RequestContext) -> BoxFuture<'static, Result<Vec<ResourceContents>>>
31        + Send
32        + Sync,
33>;
34
35/// Prompt handler function signature
36pub type PromptHandler = Box<
37    dyn Fn(
38            String,
39            Option<HashMap<String, String>>,
40            RequestContext,
41        ) -> BoxFuture<'static, Result<(Option<String>, Vec<PromptMessage>)>>
42        + Send
43        + Sync,
44>;
45
46/// Endpoint handler function signature
47pub type EndpointHandler = Arc<
48    dyn Fn(RequestContext, Option<Value>) -> BoxFuture<'static, Result<HttpResponse>> + Send + Sync,
49>;
50
51/// Multipart endpoint handler function signature
52/// Note: Multipart streams are not Send, so they must be processed immediately
53/// The returned future does not need to be Send as it's processed on the same task
54pub type MultipartEndpointHandler = Arc<
55    dyn Fn(
56            RequestContext,
57            Multipart,
58        ) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<HttpResponse>>>>
59        + Send
60        + Sync,
61>;
62
63/// Registered tool
64pub struct RegisteredTool {
65    pub meta: Tool,
66    pub handler: ToolHandler,
67}
68
69/// Registered resource
70pub struct RegisteredResource {
71    pub meta: Resource,
72    pub list_handler: ResourceListHandler,
73    pub read_handler: ResourceReadHandler,
74}
75
76/// Registered prompt
77pub struct RegisteredPrompt {
78    pub meta: Prompt,
79    pub handler: PromptHandler,
80}
81
82/// Registered endpoint
83pub struct RegisteredEndpoint {
84    pub route: String,
85    pub method: String,
86    pub description: Option<String>,
87    pub handler: EndpointHandler,
88}
89
90/// Registered multipart endpoint
91pub struct RegisteredMultipartEndpoint {
92    pub route: String,
93    pub method: String,
94    pub description: Option<String>,
95    pub handler: MultipartEndpointHandler,
96}