Skip to main content
Glama
handsomestWei

Java Class Analyzer MCP Server

scan_dependencies

Scans Maven project dependencies to create a class-to-JAR mapping index for accurate Java code analysis and dependency resolution.

Instructions

扫描Maven项目的所有依赖,建立类名到JAR包的映射索引

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
projectPathYesMaven项目根目录路径
forceRefreshNo是否强制刷新索引

Implementation Reference

  • Core logic implementing the dependency scanning: gets Maven deps, extracts classes from each JAR, builds index, caches to .mcp-class-index.json.
    async scanProject(projectPath: string, forceRefresh: boolean = false): Promise<ScanResult> {
        const indexPath = path.join(projectPath, '.mcp-class-index.json');
        const isDebug = process.env.NODE_ENV === 'development';
    
        // 如果强制刷新,先删除旧的索引文件
        if (forceRefresh && await fs.pathExists(indexPath)) {
            if (isDebug) {
                console.error('强制刷新:删除旧的索引文件');
            }
            await fs.remove(indexPath);
        }
    
        // 检查缓存
        if (!forceRefresh && await fs.pathExists(indexPath)) {
            if (isDebug) {
                console.error('使用缓存的类索引');
            }
            const cachedIndex = await fs.readJson(indexPath);
            return {
                jarCount: cachedIndex.jarCount,
                classCount: cachedIndex.classCount,
                indexPath,
                sampleEntries: cachedIndex.sampleEntries
            };
        }
    
        if (isDebug) {
            console.error('开始扫描Maven依赖...');
        }
    
        // 1. 获取Maven依赖树
        const dependencies = await this.getMavenDependencies(projectPath);
        console.error(`找到 ${dependencies.length} 个依赖JAR包`);
    
        // 2. 解析每个JAR包,建立类索引
        const classIndex: ClassIndexEntry[] = [];
        let processedJars = 0;
    
        for (const jarPath of dependencies) {
            try {
                const classes = await this.extractClassesFromJar(jarPath);
                classIndex.push(...classes);
                processedJars++;
    
                if (processedJars % 10 === 0) {
                    console.error(`已处理 ${processedJars}/${dependencies.length} 个JAR包`);
                }
            } catch (error) {
                console.warn(`处理JAR包失败: ${jarPath}, 错误: ${error}`);
            }
        }
    
        // 3. 保存索引到文件
        const result: ScanResult = {
            jarCount: processedJars,
            classCount: classIndex.length,
            indexPath,
            sampleEntries: classIndex.slice(0, 10).map(entry =>
                `${entry.className} -> ${path.basename(entry.jarPath)}`
            )
        };
    
        await fs.outputJson(indexPath, {
            ...result,
            classIndex,
            lastUpdated: new Date().toISOString()
        }, { spaces: 2 });
    
        console.error(`扫描完成!处理了 ${processedJars} 个JAR包,索引了 ${classIndex.length} 个类`);
    
        return result;
    }
  • MCP server handler for scan_dependencies tool call: extracts args, calls DependencyScanner, returns MCP-formatted response.
    private async handleScanDependencies(args: any) {
        const { projectPath, forceRefresh = false } = args;
    
        const result = await this.scanner.scanProject(projectPath, forceRefresh);
    
        return {
            content: [
                {
                    type: 'text',
                    text: `依赖扫描完成!\n\n` +
                        `扫描的JAR包数量: ${result.jarCount}\n` +
                        `索引的类数量: ${result.classCount}\n` +
                        `索引文件路径: ${result.indexPath}\n\n` +
                        `示例索引条目:\n${result.sampleEntries.slice(0, 5).join('\n')}`,
                },
            ],
        };
    }
  • Input JSON Schema for scan_dependencies tool.
    inputSchema: {
        type: 'object',
        properties: {
            projectPath: {
                type: 'string',
                description: 'Maven项目根目录路径',
            },
            forceRefresh: {
                type: 'boolean',
                description: '是否强制刷新索引',
                default: false,
            },
        },
        required: ['projectPath'],
    },
  • src/index.ts:123-125 (registration)
    Registration of tool handlers in CallToolRequestSchema switch statement.
    switch (name) {
        case 'scan_dependencies':
            return await this.handleScanDependencies(args);
  • src/index.ts:51-69 (registration)
    Tool metadata registration (name, desc, schema) in ListToolsRequestSchema handler.
    {
        name: 'scan_dependencies',
        description: '扫描Maven项目的所有依赖,建立类名到JAR包的映射索引',
        inputSchema: {
            type: 'object',
            properties: {
                projectPath: {
                    type: 'string',
                    description: 'Maven项目根目录路径',
                },
                forceRefresh: {
                    type: 'boolean',
                    description: '是否强制刷新索引',
                    default: false,
                },
            },
            required: ['projectPath'],
        },
    },

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/handsomestWei/java-class-analyzer-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server