mcpgraph 0.1.5 → 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/README.md +1 -0
  2. package/dist/api.d.ts +17 -1
  3. package/dist/api.d.ts.map +1 -1
  4. package/dist/api.js +33 -4
  5. package/dist/api.js.map +1 -1
  6. package/dist/execution/context.d.ts +3 -6
  7. package/dist/execution/context.d.ts.map +1 -1
  8. package/dist/execution/context.js +11 -2
  9. package/dist/execution/context.js.map +1 -1
  10. package/dist/execution/controller.d.ts +29 -0
  11. package/dist/execution/controller.d.ts.map +1 -0
  12. package/dist/execution/controller.js +95 -0
  13. package/dist/execution/controller.js.map +1 -0
  14. package/dist/execution/executor.d.ts +8 -1
  15. package/dist/execution/executor.d.ts.map +1 -1
  16. package/dist/execution/executor.js +184 -37
  17. package/dist/execution/executor.js.map +1 -1
  18. package/dist/execution/nodes/entry-executor.d.ts +1 -1
  19. package/dist/execution/nodes/entry-executor.d.ts.map +1 -1
  20. package/dist/execution/nodes/entry-executor.js +3 -2
  21. package/dist/execution/nodes/entry-executor.js.map +1 -1
  22. package/dist/execution/nodes/exit-executor.d.ts +1 -1
  23. package/dist/execution/nodes/exit-executor.d.ts.map +1 -1
  24. package/dist/execution/nodes/exit-executor.js +3 -2
  25. package/dist/execution/nodes/exit-executor.js.map +1 -1
  26. package/dist/execution/nodes/mcp-tool-executor.d.ts +1 -1
  27. package/dist/execution/nodes/mcp-tool-executor.d.ts.map +1 -1
  28. package/dist/execution/nodes/mcp-tool-executor.js +3 -2
  29. package/dist/execution/nodes/mcp-tool-executor.js.map +1 -1
  30. package/dist/execution/nodes/switch-executor.d.ts +1 -1
  31. package/dist/execution/nodes/switch-executor.d.ts.map +1 -1
  32. package/dist/execution/nodes/switch-executor.js +7 -2
  33. package/dist/execution/nodes/switch-executor.js.map +1 -1
  34. package/dist/execution/nodes/transform-executor.d.ts +1 -1
  35. package/dist/execution/nodes/transform-executor.d.ts.map +1 -1
  36. package/dist/execution/nodes/transform-executor.js +3 -2
  37. package/dist/execution/nodes/transform-executor.js.map +1 -1
  38. package/dist/types/execution.d.ts +92 -0
  39. package/dist/types/execution.d.ts.map +1 -0
  40. package/dist/types/execution.js +5 -0
  41. package/dist/types/execution.js.map +1 -0
  42. package/docs/design.md +220 -0
  43. package/docs/implementation.md +377 -0
  44. package/docs/introspection-debugging.md +651 -0
  45. package/examples/api-usage.ts +49 -1
  46. package/examples/switch_example.yaml +80 -0
  47. package/package.json +2 -1
@@ -0,0 +1,377 @@
1
+ # mcpGraph Implementation
2
+
3
+ This document describes the implementation of the mcpGraph MCP server with graph execution capabilities. The visual editor/UX is deferred to a later phase.
4
+
5
+ ## Overview
6
+
7
+ The implementation creates a working MCP server that can:
8
+ 1. Parse YAML configuration files
9
+ 2. Expose MCP tools defined in the configuration
10
+ 3. Execute directed graphs of nodes when tools are called
11
+ 4. Handle data transformation and routing between nodes
12
+
13
+ ## Phase 1: Foundation & Configuration Parsing
14
+
15
+ ### 1.1 Project Setup & Dependencies
16
+
17
+ **Implemented:**
18
+ - TypeScript project initialized
19
+ - Logger (stderr only) implemented in `src/logger.ts`
20
+ - All dependencies added:
21
+ - `@modelcontextprotocol/sdk` - MCP SDK
22
+ - `jsonata` - Data transformation
23
+ - `json-logic-js` - Conditional routing
24
+ - `js-yaml` - YAML parsing
25
+ - `zod` - Schema validation
26
+
27
+ **Note:** A custom execution loop was implemented to provide full control over execution flow and enable future introspection/debugging capabilities.
28
+
29
+ ### 1.2 Type Definitions
30
+
31
+ **File: `src/types/config.ts`**
32
+
33
+ All TypeScript interfaces defined:
34
+ - `McpGraphConfig` - Root configuration structure
35
+ - `ServerMetadata` - MCP server metadata
36
+ - `ToolDefinition` - Tool definition with input/output schemas (entry/exit nodes are defined in nodes with `tool` field)
37
+ - `NodeDefinition` - Base node interface
38
+ - `EntryNode` - Entry point node that receives tool arguments
39
+ - `ExitNode` - Exit point node that returns final result
40
+ - `McpNode` - MCP tool call node
41
+ - `TransformNode` - JSONata transformation node
42
+ - `SwitchNode` - JSON Logic routing node
43
+
44
+ ### 1.3 YAML Schema & Validation
45
+
46
+ **File: `src/config/schema.ts`**
47
+
48
+ Zod schemas implemented to validate YAML structure on load with clear error messages for invalid configurations.
49
+
50
+ ### 1.4 YAML Parser
51
+
52
+ **File: `src/config/parser.ts`**
53
+
54
+ YAML file parsing into structured configuration with validation against schema. Handles file I/O errors gracefully.
55
+
56
+ ### 1.5 Configuration Loader
57
+
58
+ **File: `src/config/loader.ts`**
59
+
60
+ Loads configuration from file path and returns parsed and validated configuration object.
61
+
62
+ ## Phase 2: Graph Structure & Representation
63
+
64
+ ### 2.1 Graph Data Structure
65
+
66
+ **File: `src/graph/graph.ts`**
67
+
68
+ Directed graph implementation from node definitions:
69
+ - Node adjacency (edges) storage
70
+ - Graph traversal utilities
71
+ - Support for cycles
72
+
73
+ ### 2.2 Node Registry
74
+
75
+ **Note:** Not implemented as separate file - functionality integrated into `Graph` class and `validator.ts`:
76
+ - Node ID to node definition mapping
77
+ - Node reference validation (tool references in entry/exit nodes, next, switch targets)
78
+ - Orphaned node detection
79
+ - Graph connectivity validation
80
+
81
+ ### 2.3 Graph Validator
82
+
83
+ **File: `src/graph/validator.ts`**
84
+
85
+ Graph structure validation:
86
+ - All referenced nodes exist
87
+ - All tools have exactly one entry and one exit node
88
+ - Entry nodes are only referenced as tool entry points
89
+ - Exit nodes are only referenced as tool exit points
90
+ - Exit nodes are reachable from entry nodes
91
+ - Detailed validation error messages
92
+
93
+ ## Phase 3: MCP Server Foundation
94
+
95
+ ### 3.1 MCP Server Setup
96
+
97
+ **Note:** Not implemented as separate file - functionality integrated into `src/main.ts`:
98
+ - MCP server initialization using `@modelcontextprotocol/sdk`
99
+ - Stdio transport setup
100
+ - Server lifecycle management
101
+
102
+ ### 3.2 Tool Registration
103
+
104
+ **Note:** Not implemented as separate file - functionality integrated into `src/main.ts`:
105
+ - Tool definitions converted to MCP tool schemas
106
+ - Tools registered with MCP server
107
+ - Tool names mapped to execution handlers
108
+
109
+ ### 3.3 Tool Execution Handler
110
+
111
+ **Note:** Not implemented as separate file - functionality integrated into `src/main.ts`:
112
+ - Tool invocation request handling
113
+ - Tool argument extraction
114
+ - Graph execution initiation at tool's entry node
115
+ - Result return to MCP client
116
+
117
+ ## Phase 4: Expression Engines Integration
118
+
119
+ ### 4.1 JSONata Integration
120
+
121
+ **File: `src/expressions/jsonata.ts`**
122
+
123
+ JSONata library wrapper:
124
+ - Expression evaluation with context data
125
+ - Error handling
126
+ - Support for JSONata references (e.g., `$.input.directory`)
127
+
128
+ ### 4.2 JSON Logic Integration
129
+
130
+ **File: `src/expressions/json-logic.ts`**
131
+
132
+ JSON Logic library wrapper:
133
+ - Rule evaluation with context data
134
+ - Boolean results for routing decisions
135
+ - Error handling
136
+
137
+ ### 4.3 Expression Context
138
+
139
+ **File: `src/execution/context.ts`**
140
+
141
+ Expression evaluation context building:
142
+ - Tool input arguments
143
+ - Previous node outputs
144
+ - Execution state
145
+ - Data access for expressions
146
+
147
+ **Note:** `src/expressions/context.ts` exists but functionality is primarily in `src/execution/context.ts`.
148
+
149
+ ## Phase 5: Node Execution
150
+
151
+ ### 5.1 Node Executor Base
152
+
153
+ **Note:** Not implemented as separate base class - each executor is standalone with consistent execution pattern:
154
+ - Pre-execution validation
155
+ - Execute node logic
156
+ - Post-execution processing
157
+ - Determine next node(s)
158
+
159
+ ### 5.2 MCP Tool Node Executor
160
+
161
+ **File: `src/execution/nodes/mcp-tool-executor.ts`**
162
+
163
+ MCP tool node execution:
164
+ - Pre-transform: Apply JSONata to format tool arguments
165
+ - Call MCP tool using MCP client
166
+ - Handle MCP call errors
167
+ - Return output and next node
168
+
169
+ ### 5.3 Transform Node Executor
170
+
171
+ **File: `src/execution/nodes/transform-executor.ts`**
172
+
173
+ Transform node execution:
174
+ - Apply JSONata transformation expression
175
+ - Pass through data with transformation
176
+ - Return transformed output and next node
177
+
178
+ ### 5.4 Switch Node Executor
179
+
180
+ **File: `src/execution/nodes/switch-executor.ts`**
181
+
182
+ Switch node execution:
183
+ - Evaluate JSON Logic conditions in order
184
+ - Select target node based on first matching condition
185
+ - Handle default/fallback case (conditions without rules)
186
+ - Return next node based on condition result
187
+
188
+ ### 5.5 Entry Node Executor
189
+
190
+ **File: `src/execution/nodes/entry-executor.ts`**
191
+
192
+ Entry node execution:
193
+ - Receive tool arguments from MCP client
194
+ - Initialize execution context with tool arguments
195
+ - Make tool arguments available to subsequent nodes (e.g., `$.input.*`)
196
+ - Pass execution to next node
197
+
198
+ ### 5.6 Exit Node Executor
199
+
200
+ **File: `src/execution/nodes/exit-executor.ts`**
201
+
202
+ Exit node execution:
203
+ - Extract final result from execution context
204
+ - Signal execution completion
205
+ - Return final result to MCP tool caller
206
+
207
+ ## Phase 6: Graph Execution Engine
208
+
209
+ ### 6.1 Execution Context
210
+
211
+ **File: `src/execution/context.ts`**
212
+
213
+ Execution state management:
214
+ - Current node tracking
215
+ - Data context (tool inputs, node outputs)
216
+ - Execution history
217
+ - Error state
218
+ - Data access for expressions
219
+
220
+ ### 6.2 Graph Executor
221
+
222
+ **File: `src/execution/executor.ts`**
223
+
224
+ Main graph execution orchestrator:
225
+ - Custom sequential execution loop
226
+ - Start at tool's entry node
227
+ - Execute current node based on type (entry, mcp, transform, switch, exit)
228
+ - Move to next node based on node's `next` field or switch routing
229
+ - Continue until exit node is reached
230
+ - Track execution history (node inputs/outputs)
231
+ - Handle errors with context
232
+
233
+ **Note:** The execution loop supports cycles (directed graphs with cycles), but infinite loops are prevented by the exit node check. Future loop node types can leverage this cycle support.
234
+
235
+ ### 6.3 Execution Flow
236
+
237
+ **Note:** Not implemented as separate file - functionality integrated into `executor.ts`:
238
+ - Node execution sequence coordination
239
+ - Data flow between nodes
240
+ - Branching (switch nodes)
241
+ - Parallel execution support deferred
242
+
243
+ ## Phase 7: MCP Client for External Servers
244
+
245
+ ### 7.1 MCP Client Manager
246
+
247
+ **File: `src/mcp/client-manager.ts`**
248
+
249
+ MCP client connection management:
250
+ - Connections to external MCP servers
251
+ - Client connection caching
252
+ - Connection lifecycle handling
253
+ - Support for multiple concurrent servers
254
+
255
+ ### 7.2 MCP Client Factory
256
+
257
+ **Note:** Not implemented as separate file - client creation functionality is in `client-manager.ts`:
258
+ - MCP client creation for different server types
259
+ - Stdio transport support
260
+ - Client configuration
261
+
262
+ ### 7.3 Tool Call Handler
263
+
264
+ **Note:** Not implemented as separate file - tool calling functionality is in `mcp-tool-executor.ts`:
265
+ - `callTool` request execution to external MCP servers
266
+ - Tool argument handling
267
+ - Tool response processing
268
+ - Error and timeout handling
269
+
270
+ ## Phase 8: Error Handling & Observability
271
+
272
+ ### 8.1 Error Types
273
+
274
+ **Note:** Not implemented - using standard Error types:
275
+ - Basic error handling works with standard Error
276
+ - Custom error types (ConfigError, GraphError, ExecutionError, NodeError, McpError) would improve developer experience but are not required
277
+
278
+ ### 8.2 Error Handling
279
+
280
+ **Note:** Basic error handling implemented throughout - centralized handler not implemented:
281
+ - Error logging with context (via logger)
282
+ - Error propagation through execution
283
+ - Basic user-friendly error messages
284
+ - Centralized error handler would be a nice-to-have enhancement
285
+
286
+ ### 8.3 Execution Logging
287
+
288
+ **Note:** Basic logging implemented - structured execution logger not implemented:
289
+ - Node execution events logged via basic logger
290
+ - Structured logging would be valuable for debugging but basic logger works
291
+
292
+ ## Phase 9: Integration & Testing
293
+
294
+ ### 9.1 Main Entry Point
295
+
296
+ **File: `src/main.ts`**
297
+
298
+ Main entry point implementation:
299
+ - Command-line argument parsing (config file path)
300
+ - Configuration loading
301
+ - Graph validation
302
+ - MCP server startup
303
+ - Tool registration
304
+ - Tool execution handling
305
+ - Graceful shutdown handling
306
+
307
+ ### 9.2 Integration Tests
308
+
309
+ **Files: `tests/files.test.ts`, `tests/mcp-server.test.ts`, `tests/switch.test.ts`**
310
+
311
+ Integration tests implemented:
312
+ - Full execution flow with sample configs
313
+ - Different node types tested
314
+ - MCP client integration tested
315
+ - Switch node conditional routing tested
316
+
317
+ ### 9.3 Sample Configurations
318
+
319
+ **File: `examples/count_files.yaml`**
320
+
321
+ Sample configuration implemented demonstrating:
322
+ - Tool definition
323
+ - Entry/exit nodes
324
+ - MCP tool node
325
+ - Transform node with JSONata
326
+
327
+ ## Phase 10: Polish & Documentation
328
+
329
+ ### 10.1 Code Documentation
330
+
331
+ Basic JSDoc comments present on key functions. Comprehensive documentation would be a nice-to-have enhancement.
332
+
333
+ ### 10.2 Error Messages
334
+
335
+ Basic error messages implemented. More helpful suggestions and context would improve developer experience.
336
+
337
+ ### 10.3 README Updates
338
+
339
+ **File: `README.md`**
340
+
341
+ README updated with:
342
+ - Usage instructions
343
+ - Installation from npm
344
+ - MCP server configuration examples
345
+ - Examples and configuration format documentation
346
+
347
+ ## Implementation Decisions
348
+
349
+ 1. **Custom Execution Engine**: A custom execution loop was implemented to provide full control over execution flow, enable observability (execution history), and support future debugging/introspection features.
350
+
351
+ 2. **Expression Evaluation**: All expressions (JSONata, JSON Logic) are evaluated with a consistent context that includes tool inputs and previous node outputs.
352
+
353
+ 3. **Data Flow**: Data flows through nodes as a JSON object that accumulates results. Each node can read from and write to this context.
354
+
355
+ 4. **Error Handling**: Errors at any node are caught, logged, and propagated. Basic error handling works; custom error types would be an enhancement.
356
+
357
+ 5. **MCP Client Management**: External MCP servers are managed as separate clients. The system maintains a registry of available MCP servers and their tools.
358
+
359
+ 6. **Code Organization**: Some planned separate files were integrated into existing files (e.g., server setup in main.ts, tool registration in main.ts). This works well and keeps the codebase simpler.
360
+
361
+ ## Future Considerations
362
+
363
+ See `docs/future-introspection-debugging.md` for planned introspection and debugging features.
364
+
365
+ Other future enhancements:
366
+ - Visual editor/UX
367
+ - Hot-reload of configuration
368
+ - Loop node types (for, while, foreach)
369
+ - Parallel node execution
370
+ - Retry logic for failed nodes
371
+ - Execution history persistence
372
+ - Performance monitoring/metrics
373
+ - OpenTelemetry integration
374
+ - Custom error types
375
+ - Structured execution logging
376
+ - Centralized error handler
377
+