1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//! Middleware executor abstraction for tool execution.
//!
//! This module provides a trait that abstracts tool execution with middleware,
//! enabling workflow handlers to execute tools through the same middleware
//! chain as direct tool calls without circular dependencies.
use crateResult;
use crateRequestHandlerExtra;
use async_trait;
use Value;
/// Trait for executing tools with full middleware chain application.
///
/// This trait provides an abstraction for tool execution that ensures
/// middleware (authentication, logging, validation, rate limiting, etc.)
/// is consistently applied regardless of whether the tool is called:
/// - Directly via `tools/call` JSON-RPC requests
/// - Server-side during workflow prompt execution
///
/// # Architecture
///
/// The trait decouples workflow execution from `ServerCore`, preventing
/// circular dependencies while ensuring consistent middleware application.
///
/// ```text
/// ┌─────────────────┐
/// │ WorkflowPrompt │
/// │ Handler │
/// └────────┬────────┘
/// │ uses
/// ▼
/// ┌─────────────────────┐
/// │ MiddlewareExecutor │◄─────── Trait abstraction
/// │ (trait) │
/// └────────────────────┘
/// △
/// │ implements
/// │
/// ┌────────┴────────┐
/// │ ServerCore │
/// │ (with middleware│
/// │ chains) │
/// └─────────────────┘
/// ```
///
/// # Example
///
/// ```rust,ignore
/// use pmcp::server::middleware_executor::MiddlewareExecutor;
/// use pmcp::server::workflow::WorkflowPromptHandler;
/// use std::sync::Arc;
///
/// // ServerCore implements MiddlewareExecutor
/// let executor: Arc<dyn MiddlewareExecutor> = Arc::new(server_core);
///
/// // Workflow handler uses the abstraction
/// let handler = WorkflowPromptHandler::new(
/// workflow,
/// tool_registry,
/// executor, // Pass as trait object
/// resource_handler,
/// );
///
/// // When workflow executes tools, middleware runs automatically
/// let result = handler.handle(args, extra).await?;
/// ```
///
/// # Benefits
///
/// 1. **Consistent Auth**: OAuth tokens injected by middleware work in workflows
/// 2. **Testability**: Easy to mock for workflow testing
/// 3. **No Circular Dependencies**: Clean separation of concerns
/// 4. **Single Source of Truth**: One middleware application path for all tools