A comprehensive Model Context Protocol server for advanced code analysis that provides tools for syntax analysis, dependency visualization, and AI-assisted development workflow support.
A powerful codebase analysis toolkit that leverages the Model Context Protocol (MCP) for AI-assisted code understanding and transformation.
# Clone the repository git clone https://github.com/yourusername/mcp-codeanalysis.git cd mcp-codeanalysis # Install dependencies npm install # Build the project npm run build
Redis is used for session storage in production environments. For development and testing, the system will automatically fall back to an in-memory session store if Redis is not available.
Note: There is a known issue with Redis connectivity where operations may fail even when Redis is running. See the "Tech Debt" section in
plan.md
for details. For now, you can use the./use-memory-session.sh
script to run the server with the memory session store. For more information, see Redis Troubleshooting Guide.
To install Redis:
# Ubuntu/Debian sudo apt-get install redis-server # macOS brew install redis # Windows (using WSL is recommended) # For native Windows, download from https://redis.io/download
By default, the application tries to connect to Redis at redis://localhost:6379
. You can configure the Redis connection using environment variables:
# Set custom Redis URL export REDIS_URL=redis://custom-host:6379 # Force memory session store even if Redis is available export FORCE_MEMORY_SESSION=true
# Run in development mode npm run dev # Run tests npm test # Run linting npm run lint
# Start the MCP server npm start # Run CLI tool node ./tools/mcp-stdio-client.js --task "Analyze dependencies" --files "src/*.ts"
MIT
A comprehensive Model Context Protocol (MCP) server for advanced code analysis, providing tools and insights through an extensible architecture.
# Clone the repository git clone https://github.com/your-username/codeanalysis-mcp.git cd codeanalysis-mcp # Install dependencies pnpm install # Build the project pnpm build
The CodeAnalysis MCP server can be used in two ways:
# Start the MCP server pnpm start
This starts the MCP server that can be connected to by any MCP client like Claude Desktop, Cursor, or others.
The project includes a comprehensive CLI for direct interaction:
# Get help pnpm run cli --help # Analyze a repository or directory pnpm run cli analyze repo ./src # Check code quality pnpm run cli quality analyze ./src
The project includes special tools designed to enhance AI-assisted development:
# Generate code context for AI assistants node tools/ai-dev-helper.js --task="Implement new feature" --search="related functionality" # Run example client node examples/dev-tools-client.js
See the Developer Tools Guide for detailed information.
# Analyze a local directory pnpm run cli analyze repo ./src # Analyze a specific file pnpm run cli analyze file ./src/server.ts
# Get code metrics with function details pnpm run cli metrics analyze ./src --functions # Save metrics to a file pnpm run cli metrics analyze ./src -o metrics-report.json
# Analyze dependencies in Mermaid format pnpm run cli dependencies analyze ./src -f mermaid -o deps.mmd # Visualize dependencies pnpm run cli visualize dependencies -p ./src --format mermaid
# Run quality analysis pnpm run cli quality analyze ./src # Generate HTML report pnpm run cli quality analyze ./src --html -o quality-report.html
# Build knowledge graph pnpm run cli knowledge build ./src # Query the knowledge graph pnpm run cli knowledge query ./src "type:function AND complexity>5" # Export as diagram pnpm run cli knowledge export ./src -f mermaid
# Store an insight pnpm run cli insights store -r ./src -t code-pattern -c "Refactoring opportunity" # Retrieve insights pnpm run cli insights retrieve -r ./src
# Prepare context for AI interactions node tools/ai-dev-helper.js --task="Fix authentication bug" --files="src/auth/*.ts" --search="login" # Use with AI prompts # Copy content from the generated ai-context.json file into your AI assistant prompt # or use the template in templates/ai-prompt-template.md
The project follows the MCP architecture with these components:
This server is compatible with any MCP-compliant client, including:
Commands accept paths in various formats:
./src
or /absolute/path/to/dir
./src/file.ts
or /path/to/file.ts
https://github.com/username/repo
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.
This project implements stateful tools for the Model Context Protocol (MCP) SDK, providing a framework for building tools that maintain context between invocations.
The state management architecture is organized into several modular components:
src/state/
āāā helpers/
ā āāā statefulTool.ts # Main entry point for stateful tool creation
āāā machines/
ā āāā toolMachine.ts # XState machine for tool execution flow
āāā services/
ā āāā toolService.ts # Core execution service for tools
ā āāā redisToolExecutionService.ts # Distributed execution service
ā āāā redisSessionStore.ts # Redis-based session persistence
ā āāā types.ts # Shared type definitions
statefulTool.ts
)The central integration point with the MCP SDK, providing:
// Creating a stateful tool with state persistence createStatefulTool(server, "my-tool", schema, handler); // With description createStatefulTool(server, "my-tool", "My stateful tool", schema, handler);
toolMachine.ts
)XState-based state machine that defines the execution flow for tools:
This component delegates session management to the statefulTool implementation.
toolService.ts
)Core execution service that coordinates tool state transitions:
types.ts
)Shared type definitions that ensure consistency across the state management system:
The architecture integrates with the MCP SDK by:
import { createServer } from "@modelcontextprotocol/sdk"; import { createStatefulTool } from "./state/helpers/statefulTool"; import { z } from "zod"; const server = createServer(); // Register a stateful tool createStatefulTool( server, "counter", "A tool that maintains a count between invocations", { action: z.enum(["increment", "decrement", "reset"]), }, async (params) => { // Get session ID from params (or a new one will be created) const sessionId = params.sessionId; // Process the action let count = 0; // Tool logic with state manipulation... return { count }; } ); server.listen(3000);
For distributed environments, the Redis-based implementations provide:
The components include comprehensive test suites to verify:
The CodeAnalysis MCP Server provides specialized tools for AI-assisted development. These tools help collect code context that can be fed to AI systems for more effective assistance.
The repository includes several client scripts in the tools/
directory:
HTTP Client (tools/http-client.js
): Connects to the MCP server via HTTP transport (default).
node tools/http-client.js --task "Your task description" --files "src/features/*.ts" --search "session"
Raw Client (tools/mcp-raw-client.js
): A simpler client that only captures server information.
node tools/mcp-raw-client.js --task "Your task description"
Simple Client (tools/simple-client.js
): Communicates with the server via stdio.
node tools/simple-client.js --task "Your task description" --files "src/features/*.ts"
All client scripts generate an ai-context.json
file in the project root. This file contains valuable context about your codebase that can be shared with AI assistants to provide better-informed responses.
A prompt template for AI assistants is available at templates/ai-prompt-template.md
. This template helps structure your requests to AI assistants with proper context from the MCP tools.
The MCP server supports two transport modes:
STDIO_TRANSPORT=true
environment variable to enable.MCP Code Analysis now features a modular session store architecture with automatic backend detection:
For more details, see the Session Store Architecture documentation.
Discover shared experiences
Shared threads will appear here, showcasing real-world applications and insights from the community. Check back soon for updates!