Skip to content

MDEGroup/Pathweave

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

8 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Pathweave

A structured knowledge extraction system for Python codebases, built on the Model Context Protocol (MCP). Pathweave parses source code into queryable entities, stores them as JSON or in PostgreSQL with pgvector embeddings, and exposes everything through MCP servers that AI agents can use for intelligent code analysis.

Overview

Pathweave transforms Python projects into structured, searchable knowledge through a multi-stage pipeline:

  1. Parse β€” Tree-sitter AST analysis extracts files, functions, classes, methods, and TODO/FIXME issues
  2. Store β€” Entities saved as JSON with search indices; Git commits and GitHub metadata integrated
  3. Migrate (optional) β€” Entities migrated to PostgreSQL with 384-dimensional vector embeddings for semantic search
  4. Serve β€” Two MCP servers expose tools to AI agents: one JSON-based (19 tools), one database-backed (6 tools with semantic search)

Quick Start

Installation

Requirements: Python 3.10+

pip install -r requirements.txt

Optional environment variables (.env file):

GITHUB_TOKEN=your_github_token        # For GitHub metadata extraction
MISTRAL_API_KEY=your_mistral_key      # For CrewAI agent tests

Basic Usage

Python API:

from src.generator import generate_resources

summary = generate_resources(
    project_path="path/to/project",
    output_dir="output",
    include_git=True,
    max_commits=50,
    include_github=True,
    github_repo="owner/repo"
)

print(summary['statistics'])
# {'total_entities': 383, 'files': 56, 'functions': 114, ...}

CLI:

python -m src.cli generate ./myproject -o output --git --github owner/repo

Full pipeline (generate + migrate to database):

python generate_and_migrate.py ./myproject --github owner/repo

Run Tests

python test/test_manual.py src         # Core pipeline validation (no API keys needed)
python test/test_crewai.py             # CrewAI agent test (requires MISTRAL_API_KEY)
python check_dependencies.py           # Verify all dependencies are installed

Architecture

Pipeline

Source Code β†’ Parser (tree-sitter) β†’ Entities (JSON) β†’ Query Engine
                                          ↓
                                   PGVector Migration β†’ PostgreSQL + Embeddings
                                          ↓
                                     MCP Servers β†’ AI Agents (CrewAI)

Core Modules (src/)

Module Purpose
parser.py Tree-sitter AST parsing. Extracts files, functions, classes, methods, issues from Python source
storage.py JSON persistence, Git commit extraction (GitPython), GitHub API integration
generator.py Pipeline orchestration β€” runs parser β†’ storage β†’ indices β†’ overview
generator_with_db.py Extends generator with automatic PostgreSQL migration
query.py In-memory query engine over JSON indices (search, filter, traverse)
cli.py Click-based CLI interface for generation
migrate_to_pgvector.py Migrates JSON entities to PostgreSQL with all-MiniLM-L6-v2 vector embeddings (384-dim)

Entity Types (src/entities/)

All entities inherit from BaseEntity with a unique ID format: type:filepath::name

Entity Source Key Data
FileEntity Parser imports, exports, line count
FunctionEntity Parser parameters, return type, docstring, complexity
ClassEntity Parser base classes, methods, attributes, decorators
MethodEntity Parser signature, parent class, decorators
IssueEntity Parser TODO/FIXME/HACK comments extracted from code
CommitEntity Git author, message, SHA, files changed, date
GitHubRepoEntity GitHub API stars, forks, language, topics
GitHubIssueEntity GitHub API title, state, labels, body
GitHubContributorEntity GitHub API commits, additions, deletions

MCP Servers (Python/)

Server Tools Backend Use Case
MCP_Server_V2.py 19 JSON files Full-featured: generation, search, commits, GitHub
MCP_Server_Database.py 6 PostgreSQL + pgvector Semantic search, vector similarity, SQL queries
local_mcp_server.py β€” Legacy format Original prototype (pre-v2)

MCP_Server_V2 tools include: generate_resources, find_entity, search_entities, get_entity_details, traverse_relations, multi_hop_query, get_statistics, search_functions, search_classes, get_class_methods, search_commits, get_commit_details, get_commit_statistics, list_commit_authors, get_github_repository, search_github_issues, get_github_issue_details, list_github_contributors, get_github_contributor_details

MCP_Server_Database tools: semantic_search, find_entity_by_id, search_by_name, get_relationships, find_commits_by_author, get_entity_statistics


Output Structure

After running generate_resources():

output/
β”œβ”€β”€ overview.json              # Project statistics and metadata
β”œβ”€β”€ entities/                  # Entity JSON files (MD5-hashed filenames)
β”‚   β”œβ”€β”€ file/                  # Source file entities
β”‚   β”œβ”€β”€ function/              # Standalone functions
β”‚   β”œβ”€β”€ class/                 # Classes
β”‚   β”œβ”€β”€ method/                # Class methods
β”‚   β”œβ”€β”€ issue/                 # TODO/FIXME issues from comments
β”‚   β”œβ”€β”€ commit/                # Git commits
β”‚   β”œβ”€β”€ github_repo/           # Repository metadata
β”‚   β”œβ”€β”€ github_issue/          # GitHub issues
β”‚   └── github_contributor/    # Contributors
└── indices/                   # Search indices for fast lookups
    β”œβ”€β”€ functions_by_name.json
    β”œβ”€β”€ classes_by_name.json
    β”œβ”€β”€ files_by_path.json
    β”œβ”€β”€ commits_by_author.json
    β”œβ”€β”€ commits_by_sha.json
    β”œβ”€β”€ github_issues_by_number.json
    β”œβ”€β”€ github_contributors_by_login.json
    └── github_repo_info.json

PGVector Database (Optional)

Setup

# Start PostgreSQL with pgvector (Docker)
docker run --name vector_database \
  -e POSTGRES_PASSWORD=GeneralPass1987 \
  -e POSTGRES_DB=vectors \
  -p 5432:5432 -d pgvector/pgvector:pg16

# Install Python packages
pip install psycopg2-binary sentence-transformers torch

Migration

# Generate + migrate in one step
python generate_and_migrate.py ./myproject

# Or programmatically
from src.generator_with_db import generate_resources_with_db
generate_resources_with_db("./myproject", auto_migrate=True)

Database Schema

PostgreSQL "vectors"
β”œβ”€β”€ entities                    # All entity data + embeddings
β”‚   β”œβ”€β”€ entity_id (PK)         # e.g. "function:src/auth.py::validate"
β”‚   β”œβ”€β”€ entity_type             # function, class, method, commit, etc.
β”‚   β”œβ”€β”€ name, defined_in
β”‚   β”œβ”€β”€ embedding vector(384)   # all-MiniLM-L6-v2 embeddings
β”‚   β”œβ”€β”€ type_specific_data      # JSONB (signature, params, docstring...)
β”‚   └── metadata                # JSONB
β”œβ”€β”€ entity_relationships        # Relationship graph (calls, inherits, contains)
β”‚   β”œβ”€β”€ source_entity_id (FK)
β”‚   β”œβ”€β”€ target_entity_id (FK)
β”‚   └── relationship_type
└── projects                    # Project metadata + statistics

Semantic Search Example

SELECT name, 1 - (embedding <=> query_vector) as similarity
FROM entities
WHERE entity_type = 'function'
ORDER BY embedding <=> query_vector
LIMIT 5;

Note: Only relationships between entities within the analyzed project are migrated. External library references (e.g., inheriting from BaseTool) are preserved in JSON but excluded from the database due to foreign key constraints.


Tests (test/)

Test What it validates Requirements
test_manual.py Full pipeline: generate β†’ query β†’ search β†’ statistics None
test_crewai.py CrewAI agent querying entities via @tool functions MISTRAL_API_KEY
test_crewai_rich.py CrewAI agent with 6 PGVector database tools (guided analysis of Rich library) MISTRAL_API_KEY, PostgreSQL
test_crewai_rich_autonomous.py Autonomous agent analysis β€” no guidance, open-ended task MISTRAL_API_KEY, PostgreSQL
test_crewai_rich_baseline.py Control experiment β€” same task with file-system tools instead of Pathweave MISTRAL_API_KEY
test_vector_search.py Standalone vector similarity search via pgvector PostgreSQL
Crew_mcp_test.py CrewAI + MCP server integration via stdio transport MISTRAL_API_KEY

Benchmarks (benchmark/)

Benchmark Purpose
benchmark_rich.py Full pipeline benchmark on Textualize/rich: clone β†’ parse β†’ migrate β†’ measure
benchmark_rich_part2.py Supplementary: database statistics collection + semantic search benchmark

CrewAI Integration

from crewai import Agent, Task, Crew, LLM
from crewai.mcp import MCPServerStdio

agent = Agent(
    role="Code Analyst",
    goal="Analyze Python codebase",
    llm=LLM(model="mistral/mistral-large-latest", api_key=api_key),
    mcps=[MCPServerStdio(command="python", args=["Python/MCP_Server_V2.py"])]
)

task = Task(
    description="Find all functions that parse Python code and explain their relationships",
    agent=agent
)

crew = Crew(agents=[agent], tasks=[task])
result = crew.kickoff()

Project Structure

Pathweave/
β”œβ”€β”€ src/                              # Core library
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ parser.py                     # Tree-sitter AST parser
β”‚   β”œβ”€β”€ storage.py                    # JSON persistence + Git/GitHub integration
β”‚   β”œβ”€β”€ generator.py                  # Pipeline orchestrator
β”‚   β”œβ”€β”€ generator_with_db.py          # Generator + PostgreSQL migration
β”‚   β”œβ”€β”€ query.py                      # Query engine (JSON indices)
β”‚   β”œβ”€β”€ cli.py                        # Click CLI
β”‚   β”œβ”€β”€ migrate_to_pgvector.py        # PGVector migration + embeddings
β”‚   └── entities/                     # Entity dataclasses
β”‚       β”œβ”€β”€ base.py                   # BaseEntity
β”‚       β”œβ”€β”€ file.py, function.py, class_.py, method.py
β”‚       β”œβ”€β”€ issue.py, commit.py
β”‚       └── github_repo.py, github_issue.py, github_contributor.py
β”‚
β”œβ”€β”€ Python/                           # MCP servers
β”‚   β”œβ”€β”€ MCP_Server_V2.py             # JSON-based server (19 tools)
β”‚   β”œβ”€β”€ MCP_Server_Database.py       # PGVector server (6 tools, semantic search)
β”‚   └── local_mcp_server.py          # Legacy server (prototype)
β”‚
β”œβ”€β”€ test/                             # Test suite
β”‚   β”œβ”€β”€ test_manual.py               # Core pipeline validation
β”‚   β”œβ”€β”€ test_crewai.py               # CrewAI + JSON tools
β”‚   β”œβ”€β”€ test_crewai_rich.py          # CrewAI + PGVector tools (guided)
β”‚   β”œβ”€β”€ test_crewai_rich_autonomous.py  # Autonomous agent experiment
β”‚   β”œβ”€β”€ test_crewai_rich_baseline.py # Baseline comparison (no Pathweave)
β”‚   β”œβ”€β”€ test_vector_search.py        # Semantic search demo
β”‚   └── Crew_mcp_test.py             # MCP stdio integration
β”‚
β”œβ”€β”€ benchmark/                        # Performance benchmarks
β”‚   β”œβ”€β”€ benchmark_rich.py            # Full pipeline benchmark (Rich library)
β”‚   └── benchmark_rich_part2.py      # DB stats + search benchmark
β”‚
β”œβ”€β”€ output/                           # Generated resources (gitignored)
β”œβ”€β”€ generate_and_migrate.py           # CLI: generate + migrate in one step
β”œβ”€β”€ check_dependencies.py             # Dependency verification
β”œβ”€β”€ requirements.txt
└── .env                              # API keys (gitignored)

Requirements

Core (required):

  • tree-sitter + tree-sitter-python β€” AST parsing
  • click β€” CLI interface

Optional integrations:

  • GitPython β€” Local Git commit extraction
  • requests + python-dotenv β€” GitHub API integration
  • mcp β€” MCP server protocol
  • crewai β€” AI agent framework

PGVector database (optional):

  • psycopg2-binary β€” PostgreSQL adapter
  • sentence-transformers β€” Vector embeddings (all-MiniLM-L6-v2)
  • torch β€” Required by sentence-transformers

See requirements.txt for pinned versions.


License

MIT License

About

πŸŽ“ Bachelor's Thesis πŸ“„ Title: "Dal Codice Sorgente alla Conoscenza Strutturata: Generazione Automatica di Risorse MCP per Agenti AI." πŸ‘¨β€πŸŽ“ Student: Joshua Raffaello Di Santo 🏫 University: UniversitΓ  Degli Studi Dell'Aquila πŸ“… Academic Year: 2025–2026

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • Python 100.0%