Skip to content

Commit 8b21bc4

Browse files
author
johnhuang316
committed
Initial commit
0 parents  commit 8b21bc4

File tree

12 files changed

+1347
-0
lines changed

12 files changed

+1347
-0
lines changed

.code_indexer/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Ignore everything in this directory
2+
*
3+
# Except this file
4+
!.gitignore

.gitignore

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Python cache files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
*.so
6+
.Python
7+
env/
8+
build/
9+
develop-eggs/
10+
dist/
11+
downloads/
12+
eggs/
13+
.eggs/
14+
lib/
15+
lib64/
16+
parts/
17+
sdist/
18+
var/
19+
*.egg-info/
20+
.installed.cfg
21+
*.egg
22+
23+
# Virtual environments
24+
venv/
25+
env/
26+
ENV/
27+
28+
# IDE files
29+
.idea/
30+
.vscode/
31+
*.swp
32+
*.swo
33+
34+
# OS specific files
35+
.DS_Store
36+
Thumbs.db

.python-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.11

README.md

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
# Code Index MCP
2+
3+
Code Index MCP is a Model Context Protocol server that enables large language models (LLMs) to index, search, and analyze code in project directories.
4+
5+
## Features
6+
7+
- Index and navigate project file structures
8+
- Search for specific patterns in code
9+
- Get detailed file summaries
10+
- Analyze code structure and complexity
11+
- Support for multiple programming languages
12+
- Persistent storage of project settings
13+
14+
## Installation
15+
16+
This project uses uv for environment management and dependency installation.
17+
18+
1. Ensure you have Python 3.10 or later installed
19+
2. Install uv (recommended):
20+
```bash
21+
# Windows
22+
powershell -c "irm https://astral.sh/uv/install.ps1 | iex"
23+
# macOS/Linux
24+
curl -LsSf https://astral.sh/uv/install.sh | sh
25+
```
26+
27+
3. Set up the environment and install dependencies:
28+
```bash
29+
# Create virtual environment and install dependencies
30+
uv venv
31+
uv pip install -r requirements.txt
32+
```
33+
34+
## Usage
35+
36+
### Running the Server
37+
38+
```bash
39+
# Using Python from the virtual environment
40+
.venv/Scripts/python server.py # Windows
41+
.venv/bin/python server.py # macOS/Linux
42+
43+
# Or more conveniently with uv
44+
uv run server.py
45+
```
46+
47+
### Integrating with Claude Desktop
48+
49+
You can manually configure Claude Desktop to integrate with Code Index MCP:
50+
51+
1. Ensure you have completed the environment setup as described above
52+
53+
2. Find or create the Claude Desktop configuration file:
54+
- Windows: `%APPDATA%\Claude\claude_desktop_config.json`
55+
- macOS: `~/Library/Application Support/Claude/claude_desktop_config.json`
56+
57+
3. Add the following configuration (replace with your actual path):
58+
```json
59+
{
60+
"mcpServers": {
61+
"code-indexer": {
62+
"command": "uv",
63+
"args": ["run", "C:/path/to/your/code-index-mcp/server.py"]
64+
}
65+
}
66+
}
67+
```
68+
69+
4. Restart Claude Desktop to use Code Indexer for analyzing code projects
70+
71+
### Basic Workflow
72+
73+
1. **Set Project Path** (required first step):
74+
- When using for the first time, you must set the project path to analyze
75+
- Through Claude command: "I need to analyze a project, help me set up the project path"
76+
- Provide the complete project directory path
77+
78+
2. **Code Search**:
79+
- Search for specific keywords or patterns: "Search for 'function name' in the project"
80+
- Filter by file type: "Search for 'import' in all .py files"
81+
82+
3. **File Analysis**:
83+
- Analyze specific files: "Analyze the file src/main.py"
84+
- Get file summaries: "Give me a list of functions in utils/helpers.js"
85+
86+
4. **Project Navigation**:
87+
- View project structure: "Show me the structure of this project"
88+
- Find files matching specific patterns: "Find all test_*.py files"
89+
90+
## Technical Details
91+
92+
### Persistent Storage
93+
94+
All index and settings data are stored in the `.code_indexer` folder within the project directory:
95+
- `config.json`: Project configuration information
96+
- `file_index.pickle`: File index data
97+
- `content_cache.pickle`: File content cache
98+
99+
This ensures that the entire project doesn't need to be re-indexed each time it's used.
100+
101+
### Supported File Types
102+
103+
The following file types are currently supported for indexing and analysis:
104+
- Python (.py)
105+
- JavaScript/TypeScript (.js, .ts, .jsx, .tsx)
106+
- Java (.java)
107+
- C/C++ (.c, .cpp, .h, .hpp)
108+
- C# (.cs)
109+
- Go (.go)
110+
- Ruby (.rb)
111+
- PHP (.php)
112+
- Swift (.swift)
113+
- Kotlin (.kt)
114+
- Rust (.rs)
115+
- Scala (.scala)
116+
- Shell (.sh, .bash)
117+
- HTML/CSS (.html, .css, .scss)
118+
- Markdown (.md)
119+
- JSON (.json)
120+
- XML (.xml)
121+
- YAML (.yml, .yaml)
122+
123+
## Security Considerations
124+
125+
- File path validation prevents directory traversal attacks
126+
- Absolute path access is not allowed
127+
- Project path must be explicitly set, with no default value
128+
129+
## Contributing
130+
131+
Contributions via issues or pull requests to add new features or fix bugs are welcome.
132+
133+
---
134+
135+
*For documentation in Chinese, please see [README_zh.md](README_zh.md).*

README_zh.md

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
# Code Index MCP
2+
3+
Code Index MCP 是一個基於 Model Context Protocol 的服務器,允許大型語言模型 (LLMs) 索引、搜索和分析專案目錄中的程式碼。
4+
5+
## 功能特點
6+
7+
- 索引並導航專案文件結構
8+
- 搜索特定模式的程式碼
9+
- 獲取文件的詳細摘要
10+
- 分析程式碼結構和複雜性
11+
- 支持多種程式語言
12+
- 專案設定持久化存儲
13+
14+
## 安裝
15+
16+
本專案使用 uv 進行環境管理和依賴安裝。
17+
18+
1. 確保您已安裝 Python 3.10 或更高版本
19+
2. 安裝 uv (推薦):
20+
```bash
21+
# Windows
22+
powershell -c "irm https://astral.sh/uv/install.ps1 | iex"
23+
# macOS/Linux
24+
curl -LsSf https://astral.sh/uv/install.sh | sh
25+
```
26+
27+
3. 設置環境並安裝依賴:
28+
```bash
29+
# 創建虛擬環境和安裝依賴
30+
uv venv
31+
uv pip install -r requirements.txt
32+
```
33+
34+
## 使用方法
35+
36+
### 運行服務器
37+
38+
```bash
39+
# 使用虛擬環境中的 Python 執行
40+
.venv/Scripts/python server.py # Windows
41+
.venv/bin/python server.py # macOS/Linux
42+
43+
# 或使用 uv 更簡便地執行
44+
uv run server.py
45+
```
46+
47+
### 與 Claude Desktop 整合
48+
49+
您可以手動配置 Claude Desktop 使其與 Code Index MCP 整合:
50+
51+
1. 確保已按上述步驟完成環境安裝
52+
53+
2. 找到或創建 Claude Desktop 配置文件:
54+
- Windows: `%APPDATA%\Claude\claude_desktop_config.json`
55+
- macOS: `~/Library/Application Support/Claude/claude_desktop_config.json`
56+
57+
3. 添加以下配置 (請替換為您的實際路徑):
58+
```json
59+
{
60+
"mcpServers": {
61+
"code-indexer": {
62+
"command": "uv",
63+
"args": ["run", "C:/path/to/your/code-index-mcp/server.py"]
64+
}
65+
}
66+
}
67+
```
68+
69+
4. 重啟 Claude Desktop,就可以使用 Code Indexer 來分析代碼專案
70+
71+
### 基本使用流程
72+
73+
1. **設定專案路徑** (必要第一步):
74+
- 首次使用時,必須先設定要分析的專案路徑
75+
- 透過 Claude 指令: "我需要分析一個專案,幫我設定專案路徑"
76+
- 提供完整的專案目錄路徑
77+
78+
2. **代碼搜索**:
79+
- 搜索特定關鍵字或模式: "在專案中搜索 'function name'"
80+
- 支援按文件類型過濾: "搜索所有 .py 文件中的 'import'"
81+
82+
3. **文件分析**:
83+
- 分析特定文件: "分析 src/main.py 這個文件"
84+
- 獲取文件摘要: "給我 utils/helpers.js 的函數列表"
85+
86+
4. **專案導航**:
87+
- 查看專案結構: "顯示這個專案的結構"
88+
- 查找特定模式的文件: "找出所有的 test_*.py 文件"
89+
90+
## 技術細節
91+
92+
### 持久化存儲
93+
94+
所有索引和設定數據存儲在專案目錄下的 `.code_indexer` 文件夾中:
95+
- `config.json`: 專案配置資訊
96+
- `file_index.pickle`: 檔案索引數據
97+
- `content_cache.pickle`: 檔案內容緩存
98+
99+
這確保了不需要在每次使用時重新索引整個專案。
100+
101+
### 支持的文件類型
102+
103+
目前支持以下文件類型的索引和分析:
104+
- Python (.py)
105+
- JavaScript/TypeScript (.js, .ts, .jsx, .tsx)
106+
- Java (.java)
107+
- C/C++ (.c, .cpp, .h, .hpp)
108+
- C# (.cs)
109+
- Go (.go)
110+
- Ruby (.rb)
111+
- PHP (.php)
112+
- Swift (.swift)
113+
- Kotlin (.kt)
114+
- Rust (.rs)
115+
- Scala (.scala)
116+
- Shell (.sh, .bash)
117+
- HTML/CSS (.html, .css, .scss)
118+
- Markdown (.md)
119+
- JSON (.json)
120+
- XML (.xml)
121+
- YAML (.yml, .yaml)
122+
123+
## 安全考量
124+
125+
- 檔案路徑驗證防止目錄遍歷攻擊
126+
- 不允許透過絕對路徑存取文件
127+
- 專案路徑必須明確設定,無預設值
128+
129+
## 貢獻
130+
131+
歡迎提交問題或拉取請求,以添加新功能或修復錯誤。

example_client.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
"""
2+
Example client script for the Code Index MCP Server.
3+
4+
This script shows how to programmatically interact with the Code Index MCP Server.
5+
"""
6+
import asyncio
7+
import json
8+
from mcp import ClientSession, StdioServerParameters
9+
from mcp.client.stdio import stdio_client
10+
11+
async def main():
12+
# Create server parameters for stdio connection
13+
server_params = StdioServerParameters(
14+
command="python",
15+
args=["server.py"],
16+
)
17+
18+
async with stdio_client(server_params) as (read, write):
19+
async with ClientSession(read, write) as session:
20+
# Initialize the connection
21+
await session.initialize()
22+
23+
print("Connected to Code Index MCP Server\n")
24+
25+
# List available tools
26+
print("Available Tools:")
27+
tools = await session.list_tools()
28+
for tool in tools:
29+
print(f" - {tool['name']}: {tool['description']}")
30+
print()
31+
32+
# Set project path (use a known code directory)
33+
print("Setting project path...")
34+
result = await session.call_tool(
35+
"set_project_path",
36+
arguments={"path": "."} # Index the current directory
37+
)
38+
print(f"Result: {result}\n")
39+
40+
# Get project structure
41+
print("Getting project structure...")
42+
content, _ = await session.read_resource("structure://project")
43+
structure = json.loads(content)
44+
print(f"Project structure: {len(structure)} top-level items\n")
45+
46+
# Search for some code
47+
print("Searching for 'mcp.tool'...")
48+
results = await session.call_tool(
49+
"search_code",
50+
arguments={
51+
"query": "mcp.tool",
52+
"extensions": [".py"],
53+
"case_sensitive": False
54+
}
55+
)
56+
print(f"Found {len(results)} files with matches:")
57+
for file, matches in results.items():
58+
print(f" - {file}: {len(matches)} matches")
59+
print()
60+
61+
# Get a file summary
62+
print("Getting file summary for server.py...")
63+
summary = await session.call_tool(
64+
"get_file_summary",
65+
arguments={"file_path": "server.py"}
66+
)
67+
print(f"File summary:")
68+
print(f" - Lines: {summary.get('line_count')}")
69+
print(f" - Functions: {summary.get('function_count', 'N/A')}")
70+
print(f" - Classes: {summary.get('class_count', 'N/A')}")
71+
print()
72+
73+
if __name__ == "__main__":
74+
asyncio.run(main())

0 commit comments

Comments
 (0)