Skip to content

Commit d02be3a

Browse files
authored
Merge pull request #91 from wellcode-ai/update_new_version
Update new version
2 parents 74fa82c + aaf7e4a commit d02be3a

46 files changed

Lines changed: 12364 additions & 1675 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CLAUDE.md

Lines changed: 123 additions & 232 deletions
Original file line numberDiff line numberDiff line change
@@ -4,256 +4,147 @@ This file provides guidance to Claude Code (claude.ai/code) when working with co
44

55
## Project Overview
66

7-
NUTS (Network Universal Testing Suite) is a Rust CLI tool for API testing, performance testing, and security scanning. It features an interactive shell with tab completion, AI-powered command suggestions, and OpenAPI flow management.
7+
NUTS (Network Universal Testing Suite) is a Rust CLI tool for MCP server testing and API testing. It operates in two modes:
8+
9+
1. **Non-interactive CLI** -- `nuts call`, `nuts mcp discover`, `nuts mcp test`, etc. Designed for scripts and CI.
10+
2. **Interactive REPL** -- `nuts shell` enters the original interactive shell with tab completion.
11+
12+
AI features (security scanning, test generation) use Anthropic's Claude API via a centralized `AiService`.
813

914
## Development Commands
1015

11-
### Build and Run
1216
```bash
1317
cargo build # Build the project
14-
cargo run # Run the CLI tool
15-
cargo install --path . # Install locally
16-
```
18+
cargo run # Run the CLI (shows help, not REPL)
19+
cargo run -- shell # Enter the interactive REPL
20+
cargo install --path . # Install locally as `nuts` binary
1721

18-
### Testing
19-
```bash
2022
cargo test # Run all tests
21-
cargo test --lib # Run library tests only
22-
cargo test --bin nuts # Run binary tests only
23+
cargo test --lib # Library tests only
24+
cargo test --bin nuts # Binary tests only
25+
cargo test <test_name> # Run a single test by name
26+
cargo test mcp # Run MCP-related tests
27+
28+
cargo fmt # Format code (CI enforces --check)
29+
cargo clippy --all-targets --all-features -- -D warnings # Lint (CI treats warnings as errors)
30+
cargo check # Quick compile check
2331
```
2432

25-
### Code Quality
26-
```bash
27-
cargo fmt # Format code
28-
cargo clippy # Run linter
29-
cargo check # Check for compile errors
30-
```
33+
## CI Requirements
34+
35+
The GitHub Actions CI pipeline (`.github/workflows/ci.yml`) runs on PRs to `main` and `develop`:
36+
- `cargo fmt -- --check` -- formatting must pass
37+
- `cargo clippy --all-targets --all-features -- -D warnings` -- no clippy warnings allowed
38+
- `cargo test --verbose`
39+
- `cargo doc --no-deps --document-private-items`
40+
- Cross-platform builds (Linux, Windows, macOS)
41+
- `cargo audit` for security vulnerabilities
3142

3243
## Architecture
3344

34-
### Core Components
45+
### Execution Flow (main.rs)
46+
47+
`main.rs` uses `clap` derive macros for CLI parsing. The `Cli` struct has a `Commands` enum with subcommands: `Call`, `Perf`, `Security`, `Ask`, `Mcp`, `Config`, `Shell`. No subcommand prints brief help.
48+
49+
Each `run_*` function creates a `tokio::runtime::Runtime` and calls `block_on` for async work. The `shell` subcommand delegates to `NutsShell::run()` (the original REPL).
50+
51+
Global flags: `--json`, `--quiet`, `--no-color`, `--verbose`, `--env`. TTY detection sets `NO_COLOR` when piped.
52+
53+
### MCP Commands (`nuts mcp <subcommand>`)
54+
55+
MCP subcommands use `McpTransportArgs` for transport selection (`--stdio`, `--sse`, `--http`). The `resolve_transport()` function converts these to a `TransportConfig` enum.
56+
57+
Working subcommands:
58+
- `nuts mcp connect --stdio "cmd"` -- connect, print server info, disconnect
59+
- `nuts mcp discover --stdio "cmd" [--json]` -- full capability listing
60+
- `nuts mcp generate --stdio "cmd" [--json]` -- AI-generate test YAML from discovered schemas
61+
62+
Placeholder subcommands (not yet wired): `test`, `perf`, `security`, `snapshot`.
3563

36-
- **`src/main.rs`** - Entry point that initializes the shell
37-
- **`src/shell.rs`** - Main shell implementation with command processing
38-
- **`src/commands/`** - Command implementations (call, perf, security, config, monitor, etc.)
39-
- **`src/flows/`** - OpenAPI flow management and collection system
40-
- **`src/models/`** - Data structures for analysis and metrics
41-
- **`src/config.rs`** - Configuration management with API key storage
42-
- **`src/completer.rs`** - Tab completion for shell commands
43-
- **`src/story/`** - AI-guided workflow system
64+
### Error Handling (`src/error.rs`)
4465

45-
### Key Features
66+
`NutsError` enum with `thiserror` + `miette`:
67+
- Variants: `Http`, `Ai`, `Config`, `Mcp`, `Protocol`, `Flow`, `AuthRequired`, `Io`, `InvalidInput`
68+
- `pub type Result<T> = std::result::Result<T, NutsError>;`
69+
- Auto-conversions from `reqwest::Error`, `serde_json::Error`, `serde_yaml::Error`, `std::io::Error`
4670

47-
1. **Interactive Shell** - Uses `rustyline` for command line editing with tab completion
48-
2. **API Testing** - HTTP client with support for all common methods
49-
3. **Performance Testing** - Concurrent load testing with configurable parameters
50-
4. **Security Scanning** - AI-powered security analysis using Anthropic's Claude
51-
5. **OpenAPI Flows** - Create, manage, and execute API collections
52-
6. **Mock Server** - Generate mock servers from OpenAPI specifications
53-
7. **Story Mode** - AI-guided API workflow exploration
54-
8. **Health Monitoring** - Real-time API health monitoring with AI insights
55-
9. **Natural Language Interface** - AI-powered command generation from natural language
71+
### MCP Module (`src/mcp/`)
5672

57-
### Configuration
73+
- **`client.rs`** -- `McpClient` wraps the `rmcp` SDK. Methods: `connect_stdio`, `connect_sse`, `connect_http`, `connect` (from `TransportConfig`), `discover`, `list_tools`, `call_tool`, `list_resources`, `read_resource`, `list_prompts`, `get_prompt`, `disconnect`.
74+
- **`types.rs`** -- Data types: `ServerCapabilities`, `Tool`, `Resource`, `ResourceTemplate`, `Prompt`, `PromptArgument`, `ToolResult`, `ContentItem` (Text/Image/Audio/Resource), `ResourceContent`, `PromptResult`, `PromptMessage`, `TransportConfig` (Stdio/Sse/Http).
75+
- **`discovery.rs`** -- `discover()` convenience function, `format_discovery_human()`, `format_discovery_json()`.
76+
- **`generate.rs`** -- `generate_tests(client, ai)` discovers tools and AI-generates YAML test cases. Handles markdown fence stripping.
77+
- **`test_runner.rs`** -- YAML test file parser and assertion engine. `TestFile`/`ServerConfig`/`TestCase`/`TestStep` structs. `run_tests(path)` connects to server, executes tests, returns `TestSummary`. Supports captures (`$.field` JSONPath) and variable references (`${var}`). Human and JSON summary formatters.
5878

59-
- Configuration stored in `~/.nuts_config.json`
60-
- Flow collections stored in `~/.nuts/flows/`
61-
- API key required for AI features (security scanning, story mode, monitoring, natural language)
79+
### AI Module (`src/ai/`)
80+
81+
- **`service.rs`** -- `AiService` holds a `Box<dyn AiProvider>`, tracks token usage, maintains conversation buffer. Methods: `complete()`, `complete_with_system()`, `chat()`, `converse()`, `generate_test_cases()`, `security_scan()`, `explain()`, `validate_output()`, `suggest_command()`. Default model: `claude-sonnet-4-5-20250929`.
82+
- **`provider.rs`** -- `AiProvider` trait + `AnthropicProvider`. `MockProvider` for tests.
83+
- **`prompts.rs`** -- All prompt templates as functions. MCP-specific: `mcp_test_generation()`, `mcp_security_scan()`, `mcp_output_validation()`. API: `api_security_analysis()`, `command_suggestion()`, `explain_response()`, `natural_language_command()`, etc.
84+
85+
### Output Module (`src/output/`)
86+
87+
- **`renderer.rs`** -- `render_status_line()`, `render_json_body()` (syntax highlighted), `render_headers()`, `render_table()` (comfy-table), `render_error()` (what/why/fix), `render_ai_insight()`, `render_test_result()`, `render_section()`, `spinner_style()`. `OutputMode` enum: Human/Json/Junit/Quiet.
88+
- **`colors.rs`** -- Semantic color system using `console` crate. `init_colors()`, `colors_enabled()`. Styles: success, warning, error, info, muted, accent (+ bold). JSON: json_key (cyan), json_string (green), json_number (yellow), json_bool (magenta), json_null (dim red). Respects `NO_COLOR`.
89+
- **`welcome.rs`** -- `welcome_message()` (3 lines), `first_run_message()`, `help_text()` (grouped by task: MCP TESTING, MAKING REQUESTS, etc.), `command_help(cmd)` per-command help.
90+
91+
### Legacy Modules (unchanged)
92+
93+
- **`src/shell.rs`** -- Interactive REPL with rustyline, tab completion, command dispatch via `process_command()` match block
94+
- **`src/commands/`** -- Original command implementations (call, perf, security, ask, monitor, generate, etc.)
95+
- **`src/flows/`** -- OpenAPI flow management and collection system
96+
- **`src/story/`** -- AI-guided workflow exploration
97+
- **`src/models/`** -- `ApiAnalysis`, `Metrics`, `MetricsSummary`
98+
- **`src/config.rs`** -- Config stored at `~/.nuts/config.json`
99+
- **`src/completer.rs`** -- Tab completion for shell mode
100+
101+
## Key Patterns
102+
103+
- **Error handling**: New modules use `NutsError` via `thiserror`/`miette`. Legacy code still uses `Box<dyn Error>`. `main.rs` run functions return `Box<dyn Error>` to bridge both.
104+
- **AI integration**: New code uses `AiService` (one instance, shared). Legacy commands still create per-call `anthropic::client::Client`.
105+
- **Async**: Each `run_*` function in main.rs creates its own `tokio::runtime::Runtime`. The shell creates one in `NutsShell::run()`.
106+
- **MCP transport**: All MCP commands resolve `--stdio`/`--sse`/`--http` flags to `TransportConfig`, then call `McpClient::connect()`.
107+
- **User data**: All persisted data lives under `~/.nuts/` (config, flows). No database.
108+
109+
## MCP Test YAML Format
110+
111+
Test files (`.test.yaml`) have `server:` (transport config) and `tests:` (array of test cases). See `docs/mcp-test-format.md` for the full specification. Key structures:
112+
113+
```yaml
114+
server:
115+
command: "node server.js" # or sse: / http:
116+
timeout: 30
117+
118+
tests:
119+
- name: "test name"
120+
tool: "tool_name" # or resource: / prompt:
121+
input: { key: value }
122+
assert:
123+
status: success
124+
result.type: object
125+
result.has_field: [id, name]
126+
duration_ms: { max: 5000 }
127+
capture:
128+
var_name: "$.field.path"
129+
130+
- name: "multi-step"
131+
steps:
132+
- tool: "create"
133+
input: { title: "test" }
134+
capture: { id: "$.id" }
135+
- tool: "get"
136+
input: { id: "${id}" }
137+
```
62138
63-
### Dependencies
139+
## Dependencies
64140
65-
- **UI/UX**: `ratatui`, `crossterm`, `console`, `inquire`, `dialoguer`
141+
Key crates:
142+
- **CLI**: `clap` (derive macros)
143+
- **MCP**: `rmcp` (client, transport-child-process, transport-streamable-http, transport-sse)
144+
- **AI**: `anthropic` client crate
145+
- **Error**: `thiserror`, `miette` (fancy diagnostics)
66146
- **HTTP**: `reqwest`, `axum`, `hyper`, `tower`
67-
- **AI**: `anthropic` client
68-
- **CLI**: `clap` for argument parsing, `rustyline` for shell
147+
- **Output**: `comfy-table`, `console`, `indicatif`, `crossterm`
69148
- **Serialization**: `serde`, `serde_json`, `serde_yaml`
70-
- **Async**: `tokio` runtime
71-
72-
## Complete Command Reference
73-
74-
### Core Commands
75-
76-
#### `call [OPTIONS] [METHOD] URL [BODY]`
77-
Advanced HTTP client with CURL-like features
78-
- **Options**: `-H` (headers), `-u` (basic auth), `--bearer` (token), `-v` (verbose), `-L` (follow redirects)
79-
- **Examples**:
80-
```bash
81-
call GET https://api.example.com/users
82-
call POST https://api.example.com/users '{"name": "John"}'
83-
call -H "Content-Type: application/json" -v POST https://api.example.com/users '{"name": "John"}'
84-
```
85-
86-
#### `ask "natural language request"`
87-
AI-powered natural language to API call conversion
88-
- **Examples**:
89-
```bash
90-
ask "Create a POST request with user data"
91-
ask "Get all products from the API"
92-
ask "Delete user with ID 123"
93-
```
94-
95-
#### `perf [METHOD] URL [--users N] [--duration Ns] [BODY]`
96-
Performance testing with concurrent load testing
97-
- **Options**: `--users` (concurrent users), `--duration` (test duration)
98-
- **Examples**:
99-
```bash
100-
perf GET https://api.example.com/users
101-
perf GET https://api.example.com/users --users 100 --duration 30s
102-
perf POST https://api.example.com/users --users 50 '{"name": "Test"}'
103-
```
104-
105-
#### `security URL [--deep] [--auth TOKEN] [--save FILE]`
106-
AI-powered security vulnerability scanning
107-
- **Options**: `--deep` (thorough analysis), `--auth` (authentication token), `--save` (save results)
108-
- **Examples**:
109-
```bash
110-
security https://api.example.com
111-
security https://api.example.com --deep --auth "Bearer token123"
112-
security https://api.example.com --save security_report.json
113-
```
114-
115-
#### `monitor <URL> [--smart]`
116-
Real-time API health monitoring with AI insights
117-
- **Functionality**:
118-
- Performs health checks every 30 seconds
119-
- Monitors response times and status codes
120-
- Detects issues (slow responses, errors, empty responses)
121-
- With `--smart` flag: AI analysis every 3rd check providing trend analysis, predictions, and recommendations
122-
- **Examples**:
123-
```bash
124-
monitor https://api.example.com
125-
monitor https://api.example.com --smart
126-
```
127-
128-
#### `discover <BASE_URL>`
129-
Auto-discover API endpoints and generate OpenAPI specifications
130-
- **Examples**:
131-
```bash
132-
discover https://api.example.com
133-
```
134-
135-
#### `test "description" [base_url]`
136-
AI-driven test case generation from natural language
137-
- **Examples**:
138-
```bash
139-
test "Check if user registration works"
140-
test "Verify pagination works correctly" https://api.example.com
141-
```
142-
143-
#### `generate <data_type> [count]`
144-
AI-powered realistic test data generation
145-
- **Examples**:
146-
```bash
147-
generate users 10
148-
generate products 5
149-
generate orders 20
150-
```
151-
152-
#### `predict <BASE_URL>`
153-
AI-powered API health prediction and forecasting
154-
- **Examples**:
155-
```bash
156-
predict https://api.example.com
157-
```
158-
159-
#### `explain`
160-
AI explains the last API response in human-friendly terms
161-
- **Examples**:
162-
```bash
163-
explain
164-
```
165-
166-
#### `fix <URL>`
167-
AI-powered automatic API issue detection and fixing
168-
- **Examples**:
169-
```bash
170-
fix https://api.example.com/broken-endpoint
171-
```
172-
173-
#### `config [api-key|show]`
174-
Configuration management
175-
- **Examples**:
176-
```bash
177-
config api-key
178-
config show
179-
```
180-
181-
### Flow Management Commands
182-
183-
#### `flow new <name>`
184-
Create a new OpenAPI flow collection
185-
- **Examples**:
186-
```bash
187-
flow new myapi
188-
flow new user-management
189-
```
190-
191-
#### `flow add <name> <METHOD> <path>`
192-
Add an endpoint to an existing flow
193-
- **Examples**:
194-
```bash
195-
flow add myapi GET /users
196-
flow add myapi POST /users
197-
```
198-
199-
#### `flow run <name> <endpoint>`
200-
Execute a specific endpoint from a flow
201-
- **Examples**:
202-
```bash
203-
flow run myapi /users
204-
flow run myapi /users/123
205-
```
206-
207-
#### `flow list`
208-
List all available flows
209-
- **Examples**:
210-
```bash
211-
flow list
212-
```
213-
214-
#### `flow docs <name>`
215-
Generate documentation for a flow
216-
- **Examples**:
217-
```bash
218-
flow docs myapi
219-
```
220-
221-
#### `flow mock <name> [port]`
222-
Start a mock server from OpenAPI specification
223-
- **Examples**:
224-
```bash
225-
flow mock myapi
226-
flow mock myapi 8080
227-
```
228-
229-
#### `flow story <name>`
230-
Start AI-guided interactive workflow exploration
231-
- **Examples**:
232-
```bash
233-
flow story myapi
234-
flow s myapi # shorthand
235-
```
236-
237-
#### `flow configure_mock_data <name> <endpoint>`
238-
Configure mock data for specific endpoints
239-
- **Examples**:
240-
```bash
241-
flow configure_mock_data myapi /users
242-
```
243-
244-
### Command Aliases
245-
- `c``call`
246-
- `p``perf`
247-
- `s``flow story`
248-
- `h``help`
249-
- `q``quit`
250-
251-
## Development Notes
252-
253-
- Uses async/await throughout with tokio runtime
254-
- Error handling with custom `ShellError` type
255-
- Progress indicators with `indicatif` crate
256-
- All user data stored in home directory under `.nuts/`
257-
- AI features require Anthropic API key configuration
258-
- Monitor command performs health checks every 30 seconds with optional AI analysis
259-
- Natural language commands leverage Claude AI for intelligent command generation
149+
- **Async**: `tokio`, `async-trait`
150+
- **Shell**: `rustyline`

Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ hyper = { version = "1.0", features = ["full"] }
3737
tower = "0.4"
3838
axum-server = "0.6"
3939
chrono = { version = "0.4", features = ["serde"] }
40+
thiserror = "2"
41+
miette = { version = "7", features = ["fancy"] }
42+
comfy-table = "=7.1.3"
43+
regex = "1"
44+
rmcp = { version = "0.8", features = ["client", "transport-child-process", "transport-streamable-http-client-reqwest", "reqwest", "transport-sse-client-reqwest"] }
4045
[[bin]]
4146
name = "nuts"
42-
path = "src/main.rs"
47+
path = "src/main.rs"

0 commit comments

Comments
 (0)