MCP

Aegisv1

๐Ÿค– AI Agent Support

Enabling AI Assistants to Generate High-Quality MCP Server Tests

AI Agents โ†’ Generate MCP Tests

MCP Aegis ships with dedicated AGENTS.md guides and proven prompt templates to help AI coding assistants automatically generate high-quality MCP server tests using established patterns and conventions.

AGENTS.md GuidesPrompt TemplatesAuto-scaffolding

๐Ÿ“š AGENTS.md Guide System

MCP Aegis provides dedicated AI agent guides copied directly into your project during initialization. These guides contain comprehensive instructions, examples, and best practices tailored for AI assistants.

๐ŸŽฏ Three Specialized Guides

๏ฟฝ Main AGENTS.md

Overview guide with decision matrix

๐Ÿ“ test/mcp/AGENTS.md
๐ŸŽฏ High-level guidance & approach selection

๐Ÿ“ YAML AGENTS.md

Declarative testing patterns

๐Ÿ“ test/mcp/yaml/AGENTS.md
๐ŸŽฏ 50+ patterns, examples, anti-patterns

๐Ÿ’ป Node AGENTS.md

Programmatic testing API

๐Ÿ“ test/mcp/node/AGENTS.md
๐ŸŽฏ Jest/Mocha integration, complex flows

๐Ÿš€ Auto-Installation

The guides are automatically copied to your project during initialization:

bash
npx mcp-aegis init

This copies the latest AGENTS.md files from the repository's AGENTS/ directory into your project's test structure for offline reference.

๐Ÿ“– What's Inside AGENTS.md

  • โœ… Project detection heuristics
  • โœ… Configuration templates
  • โœ… Pattern matching examples
  • โœ… Common anti-patterns to avoid
  • โœ… Workflow best practices
  • โœ… Error handling guidance

๐ŸŽฏ Target Audience

  • ๐Ÿค– AI coding assistants (Copilot, Cursor, etc.)
  • ๐Ÿ”ง Autonomous testing agents
  • โšก Code generation tools
  • ๐Ÿง  LLM-powered development workflows
  • ๐Ÿ› ๏ธ Custom automation scripts

๐Ÿ” Quick Detection Guide

AI agents should look for these patterns to identify MCP Aegis projects:

๐Ÿ—‚๏ธ File Indicators

  • aegis.config.json or *.config.json
  • .test.mcp.yml files in test directories
  • AGENTS/ directory with AI guides

โš™๏ธ Code Patterns

  • JSON-RPC with tools/list or tools/call
  • Imports from mcp-aegis
  • MCP protocol handshake sequences

๐Ÿ”„ AI Agent Workflow Summary

Follow this workflow with the AGENTS.md guides as your primary reference:

1

๐Ÿ” Detect MCP patterns โ†’ Run npx mcp-aegis init

Copies AGENTS.md guides into your project

2

๐Ÿ“– Read test/mcp/AGENTS.md for overview

Understand approach selection and conventions

3

๐Ÿ“ Reference test/mcp/yaml/AGENTS.md or test/mcp/node/AGENTS.md

Follow specific guide for your chosen testing approach

4

๐Ÿงช Generate tests using patterns from guides

Apply examples, patterns, and anti-patterns from AGENTS.md

5

โœ… Validate with npm scripts โ†’ Stop when coverage complete

Handshake + discovery + at least one tool execution

๐ŸŽฏ Advanced Pattern Matching

Leverage MCP Aegis's 50+ pattern types for precise validation:

๐Ÿ“Š Common Patterns

"match:arrayLength:3" - Exact length
"match:contains:MCP" - String contains
"match:type:array" - Type validation
"match:not:arrayLength:0" - Pattern negation

๐Ÿ”ง Advanced Patterns

"match:arrayContains:name:read_file" - Object field matching
"match:regex:\\d+ files" - Regex validation
"match:extractField:tools.*.name" - Field extraction
"match:dateAfter:2023-01-01" - Date comparisons

Pro Tip: Use Pattern Matching Guide for complete reference with examples and edge cases.

๐Ÿง  Ready-to-Use Prompt Templates

Use these battle-tested prompts to generate consistent, high-quality MCP tests. Each template is designed to work with the AGENTS.md guides and established conventions.

๐Ÿ” MCP Project Detection

Identify and initialize MCP Aegis projects

markdown
You are an AI assistant helping to set up MCP (Model Context Protocol) testing for a project.

**TASK**: Analyze this project and determine if it's an MCP server that needs MCP Aegis testing setup.

**DETECTION CHECKLIST**:
โ–ก Look for aegis.config.json or *.config.json files with MCP server configuration
โ–ก Search for .test.mcp.yml files in test/ or tests/ directories
โ–ก Check for JSON-RPC code patterns with "tools/list" or "tools/call" methods
โ–ก Find imports from 'mcp-aegis' package in existing code
โ–ก Look for AGENTS/ directory in the repository root

**IF MCP PATTERNS FOUND**:
1. Run initialization: `npx mcp-aegis init`
2. Verify the created structure:
   - test/mcp/AGENTS.md (main overview guide)
   - test/mcp/yaml/AGENTS.md (YAML testing patterns)
   - test/mcp/node/AGENTS.md (programmatic API guide)
3. Update aegis.config.json with actual server details:
   - Correct command/args for server startup
   - Proper working directory if needed
   - Environment variables if required

**OUTPUT**: 
- Report detection results with specific evidence found
- List next steps based on findings
- If MCP patterns found, proceed with configuration setup
- If no patterns found, explain this is not an MCP project

๐Ÿ“ YAML Test Generation

Generate declarative YAML tests with proper patterns

markdown
You are an expert at generating MCP Aegis YAML test files. Create a comprehensive test file that validates an MCP server.

**REQUIREMENTS**:
1. **File Location**: Save as test/mcp/yaml/[server-name]-validation.test.mcp.yml
2. **Structure**: Use proper YAML format with description and tests array
3. **Coverage**: Include both tools/list and tools/call validation

**YAML TEMPLATE**:
```yaml
description: "Comprehensive MCP server validation tests"
tests:
  # Test 1: Validate server lists available tools
  - it: "should successfully list all available tools"
    request:
      jsonrpc: "2.0"
      id: "tools-list-1"
      method: "tools/list"
      params: {}
    expect:
      response:
        jsonrpc: "2.0"
        id: "tools-list-1"
        result:
          tools: "match:not:arrayLength:0"  # Ensure tools array is not empty
      stderr: "toBeEmpty"  # No errors in stderr

  # Test 2: Validate specific tool execution
  - it: "should execute [TOOL_NAME] tool successfully"
    request:
      jsonrpc: "2.0"
      id: "tool-call-1"
      method: "tools/call"
      params:
        name: "[TOOL_NAME]"  # Replace with actual tool name
        arguments:
          # Add tool-specific arguments here
    expect:
      response:
        jsonrpc: "2.0"
        id: "tool-call-1"
        result:
          content: "match:type:array"  # Validate content structure
          isError: false
      stderr: "toBeEmpty"
```

**CRITICAL PATTERNS TO USE**:
- "match:not:arrayLength:0" for non-empty arrays
- "match:type:array" for array validation
- "match:contains:text" for string content validation
- "toBeEmpty" for stderr validation

**AVOID THESE ANTI-PATTERNS**:
โŒ Duplicate YAML keys (overwrites previous values)
โŒ Hard-coding entire response objects
โŒ Missing stderr validation
โŒ Mixing pattern types in same object

**CUSTOMIZATION**: Replace [TOOL_NAME] and arguments with actual values from the server's tools/list response.

๐Ÿ’ป Programmatic Test Generation

Create Node.js tests with proper lifecycle management

markdown
You are an expert at creating Node.js test files for MCP servers using the MCP Aegis programmatic API.

**TASK**: Create a comprehensive programmatic test file that validates MCP server functionality.

**FILE TEMPLATE**:
```javascript
import { test, before, after, beforeEach, describe } from 'node:test';
import { strict as assert } from 'node:assert';
import { connect } from 'mcp-aegis';

describe('MCP Server Integration Tests', () => {
  let client;

  // Setup: Connect to MCP server once
  before(async () => {
    client = await connect('./aegis.config.json');
    console.log('โœ… Connected to MCP server');
  });

  // CRITICAL: Clear buffers between tests to prevent interference
  beforeEach(() => {
    client.clearAllBuffers();
  });

  // Cleanup: Disconnect after all tests
  after(async () => {
    if (client) {
      await client.disconnect();
      console.log('โœ… Disconnected from MCP server');
    }
  });

  test('should successfully list available tools', async () => {
    const tools = await client.listTools();
    
    // Validate tools array structure
    assert.ok(Array.isArray(tools), 'Tools should be an array');
    assert.ok(tools.length > 0, 'Should have at least one tool available');
    
    // Validate each tool has required properties
    tools.forEach(tool => {
      assert.ok(typeof tool.name === 'string', 'Tool should have a name');
      assert.ok(typeof tool.description === 'string', 'Tool should have a description');
      assert.ok(typeof tool.inputSchema === 'object', 'Tool should have input schema');
    });
    
    console.log(`โœ… Found ${tools.length} tools: ${tools.map(t => t.name).join(', ')}`);
  });

  test('should execute [TOOL_NAME] tool successfully', async () => {
    const result = await client.callTool('[TOOL_NAME]', {
      // Add tool-specific arguments here
    });
    
    // Validate response structure
    assert.ok(Array.isArray(result.content), 'Result content should be an array');
    assert.strictEqual(result.isError, false, 'Tool execution should not have errors');
    
    // Add tool-specific validations here
    console.log('โœ… Tool executed successfully');
  });

  test('should handle invalid tool calls gracefully', async () => {
    try {
      await client.callTool('nonexistent_tool', {});
      assert.fail('Should have thrown an error for invalid tool');
    } catch (error) {
      assert.ok(error.message.includes('tool'), 'Error should mention tool issue');
      console.log('โœ… Invalid tool call handled correctly');
    }
  });
});
```

**CRITICAL REQUIREMENTS**:
1. **File Location**: Save as test/mcp/node/[server-name]-integration.programmatic.test.js
2. **Buffer Management**: ALWAYS include `beforeEach(() => client.clearAllBuffers());`
3. **Lifecycle**: Proper before/after hooks for connection management
4. **Error Handling**: Test both success and failure scenarios

**CUSTOMIZATION STEPS**:
1. Replace [TOOL_NAME] with actual tool names from your server
2. Add appropriate arguments for each tool test
3. Include tool-specific validation logic
4. Test edge cases and error conditions

**RUN COMMAND**: `node --test test/mcp/node/**/*.programmatic.test.js`

โš™๏ธ Configuration Setup

Create proper aegis.config.json

markdown
You are setting up MCP Aegis configuration for an MCP server project.

**TASK**: Create or update aegis.config.json with proper MCP server configuration.

**CONFIGURATION TEMPLATE**:
```json
{
  "name": "My MCP Server",
  "command": "node",
  "args": ["./server.js"],
  "cwd": "./",
  "startupTimeout": 5000,
  "env": {
    "NODE_ENV": "test"
  }
}
```

**FIELD EXPLANATIONS**:

**Required Fields**:
- `name`: Human-readable server name for test reports
- `command`: Executable command (node, python, ./binary, etc.)
- `args`: Array of arguments passed to the command

**Optional Fields**:
- `cwd`: Working directory for server execution (default: "./")
- `env`: Environment variables as key-value object
- `startupTimeout`: Max milliseconds to wait for startup (default: 10000)
- `readyPattern`: Regex pattern in stderr indicating server ready (avoid if possible)

**COMMON CONFIGURATIONS**:

**Node.js Server**:
```json
{
  "name": "Node MCP Server",
  "command": "node",
  "args": ["./src/server.js", "--port", "3000"],
  "cwd": "./",
  "startupTimeout": 8000
}
```

**Python Server**:
```json
{
  "name": "Python MCP Server", 
  "command": "python",
  "args": ["./server.py", "--debug"],
  "cwd": "./server",
  "env": {
    "PYTHONPATH": "./src",
    "DEBUG": "true"
  },
  "startupTimeout": 10000
}
```

**Binary/Executable**:
```json
{
  "name": "Go MCP Server",
  "command": "./bin/mcp-server",
  "args": ["--config", "config.json"],
  "cwd": "./",
  "startupTimeout": 3000
}
```

**VALIDATION STEPS**:
1. Save configuration as `aegis.config.json` in project root
2. Test configuration: `aegis query --config ./aegis.config.json`
3. Verify server starts and responds to tools/list
4. Adjust timeout/args as needed based on server behavior

**TROUBLESHOOTING**:
- If server takes long to start, increase `startupTimeout`
- If server needs specific working directory, set `cwd`
- If server requires environment variables, add them to `env`
- Avoid `readyPattern` unless server doesn't follow MCP stdio protocol

๐Ÿ“œ npm Scripts Setup

Add convenient testing commands

markdown
You are setting up convenient npm scripts for MCP testing in a project's package.json.

**TASK**: Add comprehensive npm scripts to package.json for MCP Aegis testing workflow.

**RECOMMENDED SCRIPTS**:
```json
{
  "scripts": {
    // Core Testing Scripts
    "test:mcp:yaml": "mcp-aegis 'test*/mcp/yaml/**/*.test.mcp.yml' --config ./aegis.config.json",
    "test:mcp:node": "node --test 'test*/mcp/node/**/*.programmatic.test.js'",
    "test:mcp:all": "npm run test:mcp:yaml && npm run test:mcp:node",
    
    // Debugging & Development Scripts  
    "debug:mcp": "mcp-aegis query --config ./aegis.config.json",
    "debug:mcp:verbose": "mcp-aegis query --config ./aegis.config.json --verbose --debug",
    "debug:mcp:tools": "mcp-aegis query --config ./aegis.config.json --method tools/list",
    
    // CI/CD Scripts
    "test:mcp:ci": "mcp-aegis 'test*/mcp/yaml/**/*.test.mcp.yml' --config ./aegis.config.json --json --quiet",
    "test:mcp:errors": "mcp-aegis 'test*/mcp/yaml/**/*.test.mcp.yml' --config ./aegis.config.json --errors-only",
    
    // Development Helpers
    "mcp:init": "npx mcp-aegis init",
    "mcp:validate": "mcp-aegis 'test*/mcp/yaml/**/*.test.mcp.yml' --config ./aegis.config.json --dry-run"
  }
}
```

**SCRIPT EXPLANATIONS**:

**Core Testing**:
- `test:mcp:yaml`: Run all YAML-based MCP tests
- `test:mcp:node`: Run all programmatic Node.js tests  
- `test:mcp:all`: Run complete MCP test suite

**Interactive Debugging**:
- `debug:mcp`: Interactive tool exploration
- `debug:mcp:verbose`: Detailed debug output with timing
- `debug:mcp:tools`: Quick tools/list validation

**CI/CD Integration**:
- `test:mcp:ci`: JSON output for CI systems
- `test:mcp:errors`: Focus on error reporting

**Development Helpers**:
- `mcp:init`: Initialize MCP testing structure
- `mcp:validate`: Dry-run validation without execution

**USAGE EXAMPLES**:

**Development Workflow**:
```bash
# Run all tests during development
npm run test:mcp:all

# Debug specific tool interactively
npm run debug:mcp

# Quick validation
npm run test:mcp:yaml
```

**CI/CD Pipeline**:
```bash
# In GitHub Actions / CI
npm run test:mcp:ci

# Error-focused output
npm run test:mcp:errors
```

**Tool-Specific Testing**:
```bash
# Call specific tool for testing
mcp-aegis query read_file '{"path": "test.txt"}' --config ./aegis.config.json

# Using npm script with method syntax
npx mcp-aegis query --config ./aegis.config.json --method tools/call --params '{"name":"read_file","arguments":{"path":"test.txt"}}'
```

**INTEGRATION**: Add these scripts to the existing package.json "scripts" section, merging with any existing test scripts.

๐Ÿšซ Critical Anti-Patterns

Avoid these common mistakes that cause test failures:

โŒ YAML Structure Issues

  • Duplicate YAML keys (overwrites previous values)
  • Mixing pattern types in same object
  • Hard-coding entire arrays instead of using pattern matchers

โš ๏ธ Programmatic Issues

  • Missing client.clearAllBuffers() in beforeEach
  • Not properly disconnecting clients after tests
  • Ignoring stderr buffer in validation

โš ๏ธ Project Structure Issues

  • Creating tests outside test*/mcp/ conventions
  • Inventing alternative folder structures
  • Regenerating duplicate files instead of updating existing

๐Ÿ“š Additional Resources

๐ŸŽฏ Primary Resources (Start Here)

  • test/mcp/AGENTS.md - Main overview guide
  • test/mcp/yaml/AGENTS.md - YAML testing guide
  • test/mcp/node/AGENTS.md - Programmatic guide

โšก Generated locally by npx mcp-aegis init

โœ… Success Criteria for AI Agents

Your AI agent implementation is successful when it generates tests following the AGENTS.md guides and produces these outcomes:

๐ŸŽฏ Core Test Coverage

  • โœ… MCP handshake validation
  • โœ… Tools discovery (tools/list)
  • โœ… At least one tool execution
  • โœ… Error handling and stderr validation

๐Ÿ“Š Quality Indicators

  • โœ… Follows AGENTS.md conventions exactly
  • โœ… Uses pattern matching vs hard-coding
  • โœ… Proper npm scripts included
  • โœ… Clear, maintainable test descriptions

๐Ÿง  AI Agent Best Practices

  • ๐Ÿ“– Always reference the copied AGENTS.md files as primary documentation
  • ๐ŸŽฏ Use the provided prompt templates as starting points
  • ๐Ÿ”„ Follow the 5-step workflow for consistent results
  • โš ๏ธ Avoid the documented anti-patterns to prevent common failures
  • โœ… Validate generated tests run successfully before completion

๐ŸŽ‰ Ready to Generate MCP Tests?

Use the AGENTS.md guides and prompt templates above to generate consistent, high-quality MCP server tests.