@@ -2445,61 +2445,44 @@ impl ServerCapabilities {
24452445 }
24462446
24472447 pub fn can_handle_request ( & self , client_request : & ClientJsonrpcRequest ) -> std:: result:: Result < ( ) , RpcError > {
2448- let entity = "Server" ;
24492448 let request_method = client_request. method ( ) ;
2449+
2450+ // Helper function for creating error messages
2451+ fn create_error ( capability : & str , method : & str ) -> RpcError {
2452+ RpcError :: internal_error ( ) . with_message ( create_unsupported_capability_message ( "Server" , capability, method) )
2453+ }
2454+
24502455 match client_request {
24512456 ClientJsonrpcRequest :: SetLevelRequest ( _) if self . logging . is_none ( ) => {
2452- return Err ( RpcError :: internal_error ( ) . with_message ( create_unsupported_capability_message (
2453- entity,
2454- "logging" ,
2455- request_method,
2456- ) ) )
2457+ return Err ( create_error ( "logging" , request_method) ) ;
24572458 }
2458- ClientJsonrpcRequest :: GetPromptRequest ( _) | ClientJsonrpcRequest :: ListPromptsRequest ( _) => {
2459- if self . prompts . is_none ( ) {
2460- return Err ( RpcError :: internal_error ( ) . with_message ( create_unsupported_capability_message (
2461- entity,
2462- "prompts" ,
2463- request_method,
2464- ) ) ) ;
2465- }
2459+ ClientJsonrpcRequest :: GetPromptRequest ( _) | ClientJsonrpcRequest :: ListPromptsRequest ( _)
2460+ if self . prompts . is_none ( ) =>
2461+ {
2462+ return Err ( create_error ( "prompts" , request_method) ) ;
24662463 }
24672464
24682465 ClientJsonrpcRequest :: ListResourcesRequest ( _)
24692466 | ClientJsonrpcRequest :: ListResourceTemplatesRequest ( _)
24702467 | ClientJsonrpcRequest :: ReadResourceRequest ( _)
24712468 | ClientJsonrpcRequest :: SubscribeRequest ( _)
2472- | ClientJsonrpcRequest :: UnsubscribeRequest ( _) => {
2473- if self . resources . is_none ( ) {
2474- return Err ( RpcError :: internal_error ( ) . with_message ( create_unsupported_capability_message (
2475- entity,
2476- "resources" ,
2477- request_method,
2478- ) ) ) ;
2479- }
2469+ | ClientJsonrpcRequest :: UnsubscribeRequest ( _)
2470+ if self . resources . is_none ( ) =>
2471+ {
2472+ return Err ( create_error ( "resources" , request_method) ) ;
24802473 }
24812474
2482- ClientJsonrpcRequest :: CallToolRequest ( call_tool_request) if call_tool_request. is_task_augmented ( ) => {
2483- return Err ( RpcError :: internal_error ( ) . with_message ( create_unsupported_capability_message (
2484- entity,
2485- "Task-augmented tool call" ,
2486- request_method,
2487- ) ) ) ;
2475+ ClientJsonrpcRequest :: CallToolRequest ( call_tool_request)
2476+ if call_tool_request. is_task_augmented ( ) && !self . can_run_task_augmented_tools ( ) =>
2477+ {
2478+ return Err ( create_error ( "Task-augmented tool call" , request_method) ) ;
24882479 }
24892480
24902481 ClientJsonrpcRequest :: CallToolRequest ( _) | ClientJsonrpcRequest :: ListToolsRequest ( _) if self . tools . is_none ( ) => {
2491- return Err ( RpcError :: internal_error ( ) . with_message ( create_unsupported_capability_message (
2492- entity,
2493- "tools" ,
2494- request_method,
2495- ) ) )
2482+ return Err ( create_error ( "tools" , request_method) ) ;
24962483 }
24972484 ClientJsonrpcRequest :: CompleteRequest ( _) if self . completions . is_none ( ) => {
2498- return Err ( RpcError :: internal_error ( ) . with_message ( create_unsupported_capability_message (
2499- entity,
2500- "completions" ,
2501- request_method,
2502- ) ) ) ;
2485+ return Err ( create_error ( "completions" , request_method) ) ;
25032486 }
25042487
25052488 ClientJsonrpcRequest :: GetTaskRequest ( _)
@@ -2508,25 +2491,13 @@ impl ServerCapabilities {
25082491 | ClientJsonrpcRequest :: ListTasksRequest ( _)
25092492 if self . tasks . is_none ( ) =>
25102493 {
2511- return Err ( RpcError :: internal_error ( ) . with_message ( create_unsupported_capability_message (
2512- entity,
2513- "task" ,
2514- request_method,
2515- ) ) ) ;
2494+ return Err ( create_error ( "task" , request_method) ) ;
25162495 }
25172496 ClientJsonrpcRequest :: ListTasksRequest ( _) if !self . can_list_tasks ( ) => {
2518- return Err ( RpcError :: internal_error ( ) . with_message ( create_unsupported_capability_message (
2519- entity,
2520- "listing tasks" ,
2521- request_method,
2522- ) ) ) ;
2497+ return Err ( create_error ( "listing tasks" , request_method) ) ;
25232498 }
25242499 ClientJsonrpcRequest :: CancelTaskRequest ( _) if !self . can_cancel_tasks ( ) => {
2525- return Err ( RpcError :: internal_error ( ) . with_message ( create_unsupported_capability_message (
2526- entity,
2527- "task cancellation" ,
2528- request_method,
2529- ) ) ) ;
2500+ return Err ( create_error ( "task cancellation" , request_method) ) ;
25302501 }
25312502 _ => { }
25322503 } ;
@@ -2658,103 +2629,68 @@ impl ClientCapabilities {
26582629 }
26592630
26602631 pub fn can_handle_request ( & self , server_jsonrpc_request : & ServerJsonrpcRequest ) -> std:: result:: Result < ( ) , RpcError > {
2661- let entity = "Client" ;
26622632 let request_method = server_jsonrpc_request. method ( ) ;
2633+
2634+ // Helper function for creating error messages
2635+ fn create_error ( capability : & str , method : & str ) -> RpcError {
2636+ RpcError :: internal_error ( ) . with_message ( create_unsupported_capability_message ( "Client" , capability, method) )
2637+ }
2638+
26632639 match server_jsonrpc_request {
26642640 ServerJsonrpcRequest :: CreateMessageRequest ( create_message_request) => {
26652641 match self . sampling . as_ref ( ) {
26662642 Some ( samplig_capabilities) => {
26672643 // include_context requested but not supported
26682644 if create_message_request. params . include_context . is_some ( ) && samplig_capabilities. context . is_none ( )
26692645 {
2670- return Err ( RpcError :: internal_error ( ) . with_message ( create_unsupported_capability_message (
2671- entity,
2672- "context inclusion" ,
2673- request_method,
2674- ) ) ) ;
2646+ return Err ( create_error ( "context inclusion" , request_method) ) ;
26752647 }
26762648
26772649 if create_message_request. params . tool_choice . is_some ( ) && samplig_capabilities. tools . is_none ( ) {
2678- return Err ( RpcError :: internal_error ( ) . with_message ( create_unsupported_capability_message (
2679- entity,
2680- "tool_choice" ,
2681- request_method,
2682- ) ) ) ;
2650+ return Err ( create_error ( "tool choice" , request_method) ) ;
26832651 }
26842652 }
26852653 None => {
2686- return Err ( RpcError :: internal_error ( ) . with_message ( create_unsupported_capability_message (
2687- entity,
2688- "sampling capability" ,
2689- request_method,
2690- ) ) )
2654+ return Err ( create_error ( "sampling capability" , request_method) ) ;
26912655 }
26922656 }
26932657
26942658 if create_message_request. params . is_task_augmented ( ) && !self . can_accept_sampling_task ( ) {
2695- return Err ( RpcError :: internal_error ( ) . with_message ( create_unsupported_capability_message (
2696- entity,
2697- "sampling task" ,
2698- request_method,
2699- ) ) ) ;
2659+ return Err ( create_error ( "sampling task" , request_method) ) ;
27002660 }
27012661 }
27022662 ServerJsonrpcRequest :: ListRootsRequest ( _) => {
27032663 if self . roots . is_none ( ) {
2704- return Err ( RpcError :: internal_error ( ) . with_message ( create_unsupported_capability_message (
2705- entity,
2706- "roots capability" ,
2707- request_method,
2708- ) ) ) ;
2664+ return Err ( create_error ( "roots capability" , request_method) ) ;
27092665 }
27102666 }
27112667 ServerJsonrpcRequest :: GetTaskRequest ( _) | ServerJsonrpcRequest :: GetTaskPayloadRequest ( _) => {
27122668 if self . tasks . is_none ( ) {
2713- return Err ( RpcError :: internal_error ( ) . with_message ( create_unsupported_capability_message (
2714- entity,
2715- "Task" ,
2716- request_method,
2717- ) ) ) ;
2669+ return Err ( create_error ( "Task" , request_method) ) ;
27182670 }
27192671 }
27202672 ServerJsonrpcRequest :: CancelTaskRequest ( _) => {
27212673 if let Some ( tasks) = self . tasks . as_ref ( ) {
27222674 if !tasks. can_cancel_tasks ( ) {
2723- return Err ( RpcError :: internal_error ( ) . with_message ( create_unsupported_capability_message (
2724- entity,
2725- "task cancellation" ,
2726- request_method,
2727- ) ) ) ;
2675+ return Err ( create_error ( "task cancellation" , request_method) ) ;
27282676 }
27292677 }
27302678 }
27312679 ServerJsonrpcRequest :: ListTasksRequest ( _) => {
27322680 if let Some ( tasks) = self . tasks . as_ref ( ) {
27332681 if !tasks. can_list_tasks ( ) {
2734- return Err ( RpcError :: internal_error ( ) . with_message ( create_unsupported_capability_message (
2735- entity,
2736- "listing tasks" ,
2737- request_method,
2738- ) ) ) ;
2682+ return Err ( create_error ( "listing tasks" , request_method) ) ;
27392683 }
27402684 }
27412685 }
27422686
27432687 ServerJsonrpcRequest :: ElicitRequest ( elicit_request) => {
27442688 if self . elicitation . is_none ( ) {
2745- return Err ( RpcError :: internal_error ( ) . with_message ( create_unsupported_capability_message (
2746- entity,
2747- "input elicitation" ,
2748- request_method,
2749- ) ) ) ;
2689+ return Err ( create_error ( "input elicitation" , request_method) ) ;
27502690 }
27512691
27522692 if elicit_request. params . is_task_augmented ( ) && !self . can_accept_elicitation_task ( ) {
2753- return Err ( RpcError :: internal_error ( ) . with_message ( create_unsupported_capability_message (
2754- entity,
2755- "elicitation task" ,
2756- request_method,
2757- ) ) ) ;
2693+ return Err ( create_error ( "elicitation task" , request_method) ) ;
27582694 }
27592695 }
27602696 _ => { }
0 commit comments