UNPKG

perfai-test-1-mcp-server

Version:

PerfAI Model Context Protocol (MCP) server providing security analysis tools and Auth0 authentication.

287 lines (216 loc) โ€ข 8.3 kB
# PerfAI MCP Server A specialized Model Context Protocol (MCP) server for **PerfAI Security Analysis** with Auth0 authentication. ## ๐Ÿ“ฆ Installation (from npm) Install globally (CLI): ```bash npm install -g @perfai/mcp-server ``` Or add as a project dependency: ```bash npm install @perfai/mcp-server --save-dev ``` ## ๐Ÿš€ Run (CLI) ```bash perfai-mcp-server ``` This starts the MCP server over stdio. Use with an MCP client (Cursor, VS Code, MCP Inspector). ### Optional Environment Variables You can override Auth0 settings: ```bash set PERFAI_AUTH0_DOMAIN=auth.perfai.ai set PERFAI_AUTH0_CLIENT_ID=yourClientId ``` PKCE flow is enabled; no client secret is required or bundled. ## ๐Ÿงฉ Programmatic Usage ```ts import { startServer, authManager, AuthenticationManager } from '@perfai/mcp-server'; // Start server (returns a promise) await startServer(); ``` All logging is written to stderr to remain MCP-compatible. ## ๐Ÿ” Authentication & Security **๐ŸŽฏ Universal Device Code Authentication** - Works with ANY MCP client without configuration! - **๐Ÿ”‘ Auth0 Integration**: Seamless integration with `auth.perfai.ai` - **๐ŸŒ Zero Configuration**: No redirect URLs needed - works everywhere - **๐Ÿ“ฑ Device Code Flow**: Simple browser authentication like Netflix/GitHub - **๐Ÿ›ก๏ธ Session Management**: Secure token storage and automatic validation - **๐Ÿ”„ Auto-Detection**: Server automatically detects authentication completion - **๐Ÿ‘ฅ Multi-User Support**: Isolated sessions for different users ## ๐Ÿ› ๏ธ Available Tools ### ๐Ÿ”“ **Public Tools** (No authentication required) - **๐Ÿ” `login`**: Authenticate with PerfAI using OAuth2 Device Code flow - **๐Ÿ“Š `auth_status`**: Check current authentication status and user information ### ๐Ÿ”’ **Protected Tools** (Authentication required) #### ๐Ÿ‘ค **User Management** - **๐Ÿ‘ค `user_info`**: Get detailed authenticated user information - **๐Ÿšช `logout`**: Clear authentication session and logout #### ๐Ÿข **Organization Management** - **๐Ÿข `manage_organizations`**: List, select, and refresh organizations - Actions: `list`, `select`, `refresh` - **๐Ÿ“‹ `list_apis`**: List all APIs available in current organization - **๐ŸŽฏ `select_api`**: Select an API for use with other tools #### ๐Ÿ”’ **Security Analysis** - **๐Ÿ”’ `show_security_issues`**: List security vulnerabilities by organization - Supports pagination, search, and severity filtering - **๐Ÿค– `ai_fix_security_issue`**: Generate AI-powered remediation prompts - Works with issue IDs or descriptive text - **๐Ÿ“‹ `show_fixed_issues`**: View all AI-fixed security issues in current session #### ๐Ÿš€ **Security Testing** - **๐Ÿš€ `run_security_test`**: Execute comprehensive security analysis using Docker - Requires OpenAPI spec URL and local API base path - Uses PerfAI's Docker-based security testing engine ## ๐Ÿš€ Quick Start (Local Development) ### 1. Install Dependencies ```bash npm install ``` ### 2. Build the Project ```bash npm run build ``` ### 3. Start the Server (built output) ```bash npm start ``` Or directly via ts-node (optional): ```bash npx ts-node src/index.ts ``` ### 4. Test with MCP Inspector (Optional) ```bash npm run mcp:inspect ``` ## ๐Ÿ”ง MCP Client Configuration ### For Cursor Add to your `cursor_mcp_config.json`: ```json { "mcpServers": { "perfai-mcp": { "command": "node", "args": ["C:\\path\\to\\MCP_prefai\\dist\\index.js"], "env": {} } } } ``` ### For VS Code Add to your `vscode_mcp_config.json`: ```json { "mcpServers": { "perfai-mcp": { "command": "node", "args": ["C:\\path\\to\\MCP_prefai\\dist\\index.js"], "env": {} } } } ``` ## ๐Ÿงช Authentication Flow ### Step-by-Step Authentication 1. **Check Initial Status**: Run `auth_status` โ†’ Shows "Not Authenticated" 2. **Login**: Run `login` tool: - โœ… Browser automatically opens to PerfAI Auth0 device page - ๐Ÿ“ฑ You'll see a simple code (e.g., `BDJK-WHNZ`) - ๐Ÿ” Enter the code in browser and complete authentication - ๐Ÿ”„ Server automatically detects completion 3. **Verify**: Run `auth_status` โ†’ Shows user info and "Authenticated" 4. **Use Protected Tools**: All 9 protected tools now available 5. **Logout**: Run `logout` โ†’ Returns to 2 public tools only ### Security Features - **๐Ÿ”’ Zero Configuration**: No Auth0 redirect URL setup required - **๐ŸŒ Universal Compatibility**: Works with any MCP client - **โฐ Auto-Expiry**: Tokens automatically expire for security - **๐Ÿ”„ Session Isolation**: Each user session is completely isolated ## ๐Ÿ“‹ Tool Usage Examples ### Organization Management ```bash # List available organizations manage_organizations {"action": "list"} # Select an organization manage_organizations {"action": "select", "org_id": "your-org-id"} # Refresh organization list manage_organizations {"action": "refresh"} ``` ### Security Analysis ```bash # List security issues show_security_issues {"limit": 20} # Search for specific app issues show_security_issues {"search": "myapp", "limit": 10} # Generate AI fix for an issue ai_fix_security_issue {"issue_id": "issue-id-here"} # Show all fixed issues show_fixed_issues {} ``` ### API Management ```bash # List available APIs list_apis {} # Select an API for testing select_api {"api_id": "your-api-id"} ``` ### Security Testing ```bash # Run comprehensive security test run_security_test { "spec_url": "http://localhost:3000/swagger.json", "local_base_path": "http://localhost:3000" } ``` ## ๐Ÿ—๏ธ Development ### Watch Mode ```bash npm run dev ``` ### Available Scripts - `npm run build` โ€“ Compile TypeScript to JavaScript - `npm run dev` โ€“ Watch mode for development - `npm start` โ€“ Start the MCP server - `npm run mcp:inspect` โ€“ Start MCP Inspector for testing ### Project Structure ``` โ”œโ”€โ”€ src/ โ”‚ โ””โ”€โ”€ index.ts # Main MCP server implementation โ”œโ”€โ”€ dist/ # Compiled JavaScript output โ”œโ”€โ”€ package.json # Dependencies and scripts โ”œโ”€โ”€ tsconfig.json # TypeScript configuration โ”œโ”€โ”€ cursor_mcp_config.json # Cursor MCP client config โ”œโ”€โ”€ vscode_mcp_config.json # VS Code MCP client config โ””โ”€โ”€ README.md # This file ``` ## ๐Ÿ” Troubleshooting ### Publishing (Maintainers) ```bash # Bump version (choose patch|minor|major) npm version patch # Publish (scoped public) npm publish --access public ``` If you see 403 errors ensure: (1) You're logged in (`npm whoami`), (2) The `@perfai` scope exists and you have publish rights, (3) Version not already published. ### Common Issues #### Authentication Problems - **Browser doesn't open**: Manually visit the URL shown in the login response - **Code expired**: Run `login` again to get a new device code - **Stuck on authentication**: Check browser for any error messages #### Organization Issues - **No organizations found**: Run `manage_organizations {"action": "refresh"}` - **Permission denied**: Ensure your PerfAI account has organization access #### API Testing Issues - **Docker errors**: Ensure Docker Desktop is running - **Invalid URLs**: Verify your API is accessible and spec URL is valid - **Timeout**: Large APIs may take longer; check Docker logs ### Debug Mode The server logs all activity to stderr, making debugging easier: ```bash npm start 2> debug.log # Capture debug logs ``` ## ๐ŸŒŸ Features - **๐ŸŽฏ PerfAI-Focused**: Built specifically for PerfAI security workflows - **๐Ÿ” Secure Authentication**: OAuth2 Device Code flow with session management - **๐Ÿ“Š Comprehensive Analysis**: Full security issue management and AI-powered fixes - **๐Ÿณ Docker Integration**: Containerized security testing engine - **๐Ÿ”„ Real-time Updates**: Live tool list updates based on authentication state - **๐Ÿ“ฑ Universal Client Support**: Works with Cursor, VS Code, and any MCP client **๐Ÿš€ Ready to secure your APIs with PerfAI's MCP integration!**