# How to Get Code Snippets Using Codebase-Memory-MCP: A Complete Guide

> Learn how to get code snippets with Codebase-Memory-MCP. Use the get_code_snippet tool with a qualified name from your indexed repository for precise source code retrieval.

- Repository: [Martin Vogel/codebase-memory-mcp](https://github.com/DeusData/codebase-memory-mcp)
- Tags: how-to-guide
- Published: 2026-07-04

---

**Use the `get_code_snippet` MCP tool by passing a qualified name (e.g., `myproject.module.Class.method`) discovered via `search_graph` to retrieve the original source lines from the indexed repository.**

The DeusData/codebase-memory-mcp repository provides a Model Context Protocol (MCP) server that indexes your codebase into a knowledge graph, enabling precise source code retrieval. To get code snippets using Codebase-Memory-MCP, you must understand how to construct qualified names and invoke the dedicated `get_code_snippet` tool that queries the underlying SQLite graph storage.

## Understanding the `get_code_snippet` Tool

According to the source code in [`src/mcp/mcp.c`](https://github.com/DeusData/codebase-memory-mcp/blob/main/src/mcp/mcp.c) (line 4139), the `handle_get_code_snippet` function implements the core logic for this MCP tool. The tool expects a **qualified name**—a dot-delimited path that uniquely identifies a symbol in the indexed graph (e.g., `myproject.services.order.OrderProcessor.process_order`).

Internally, the implementation:

- Queries the SQLite knowledge graph table `cbm_node_t` to locate the node
- Copies the node to a temporary heap allocation via `copy_node`
- Resolves the absolute file path and verifies it resides within the indexed repository boundary through `resolve_snippet_source`
- Reads the requested line range and returns a UTF-8 sanitized JSON payload containing the source text and metadata

The user-facing documentation for this tool is generated in [`src/cli/cli.c`](https://github.com/DeusData/codebase-memory-mcp/blob/main/src/cli/cli.c) around line 486.

## Prerequisites: Finding the Qualified Name

Before retrieving snippets, you must discover the exact qualified name. Use the `search_graph` or `search_code` tools to locate symbols.

Example workflow:

1. Search for functions matching a pattern
2. Extract the `qualified_name` from the results
3. Pass it to `get_code_snippet`

## Three Ways to Retrieve Code Snippets

### Method 1: CLI (Command Line Interface)

The CLI provides a straightforward interface for single-shot queries. First, search for the symbol, then retrieve its source.

```bash

# Step 1: Find the qualified name

codebase-memory-mcp cli search_graph '{"label":"Function","name_pattern":".*process.*"}'

# Step 2: Retrieve the source snippet

codebase-memory-mcp cli get_code_snippet '{"qualified_name":"myproject.services.order.OrderProcessor.process_order"}'

```

The CLI translates the JSON-RPC call into a request to the MCP server and returns a structured JSON response:

```json
{
  "status": "ok",
  "qualified_name": "myproject.services.order.OrderProcessor.process_order",
  "file_path": "services/order/order_processor.py",
  "start_line": 42,
  "end_line": 58,
  "source": "def process_order(self, order):\n    # ... implementation ..."

}

```

### Method 2: Direct JSON-RPC Call

For programmatic access, send raw JSON-RPC messages to the MCP server's STDIN/STDOUT pipe (or HTTP bridge if enabled).

```json
{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "get_code_snippet",
  "params": {
    "qualified_name": "myproject.services.order.OrderProcessor.process_order"
  }
}

```

The server invokes `handle_get_code_snippet` (defined at line 4139 in [`src/mcp/mcp.c`](https://github.com/DeusData/codebase-memory-mcp/blob/main/src/mcp/mcp.c)) and returns the snippet metadata and source text.

### Method 3: Python Client

The official Python client available on PyPI abstracts the JSON-RPC communication.

```python
from codebase_memory_mcp import MCPClient

client = MCPClient()  # connects to the local MCP server

# Optional: Find the symbol first

results = client.search_graph(label="Function", name_pattern=".*process.*")
qn = results[0]["qualified_name"]

# Retrieve the snippet

snippet = client.get_code_snippet(qualified_name=qn)
print(snippet["source"])

```

## Optional Parameters and Advanced Usage

The `get_code_snippet` tool supports several optional parameters to refine the output:

- **`include_neighbors`**: Set to `true` to include surrounding function or class definitions in the response
- **`start_line` / `end_line`**: Specify 1-based line numbers to restrict the snippet to a specific range
- **`raw` (CLI flag)**: Use `--raw` to output the raw snippet without the JSON wrapper

Example with line range restriction:

```bash
codebase-memory-mcp cli get_code_snippet '{"qualified_name":"myproject.utils.helper","start_line":10,"end_line":25}'

```

## Handling Ambiguous Results

When a qualified name matches multiple symbols (e.g., overloaded functions or duplicate names across modules), the tool returns an **"ambiguous"** status. The response includes a `snippet_suggestions` array (see implementation at line 4445 in [`src/mcp/mcp.c`](https://github.com/DeusData/codebase-memory-mcp/blob/main/src/mcp/mcp.c)) containing possible matches.

Review the suggestions, select the correct `qualified_name`, and retry the request with the specific identifier.

## Summary

- **Use `get_code_snippet`**: The primary MCP tool for retrieving source code from the indexed graph
- **Require qualified names**: Always discover the exact symbol path using `search_graph` or `search_code` first
- **Three access patterns**: CLI for manual queries, JSON-RPC for custom integrations, Python client for application development
- **Core implementation**: Located in [`src/mcp/mcp.c`](https://github.com/DeusData/codebase-memory-mcp/blob/main/src/mcp/mcp.c) (`handle_get_code_snippet`, `resolve_snippet_source`)
- **Handle ambiguity**: Watch for "ambiguous" status and use `snippet_suggestions` to disambiguate targets

## Frequently Asked Questions

### What is a qualified name in Codebase-Memory-MCP?

A qualified name is a dot-delimited identifier that uniquely represents a symbol in the codebase knowledge graph (e.g., `package.module.Class.method`). You must obtain this exact string from `search_graph` results before calling `get_code_snippet`, as the tool uses it to query the `cbm_node_t` table in the underlying SQLite database.

### Can I retrieve code snippets from files outside the indexed repository?

No. The `resolve_snippet_source` function in [`src/mcp/mcp.c`](https://github.com/DeusData/codebase-memory-mcp/blob/main/src/mcp/mcp.c) explicitly verifies that the resolved absolute file path resides within the indexed repository boundaries. If the file falls outside the indexed scope, the request will fail as a security measure.

### How do I get more context around a specific function?

Set the `include_neighbors` parameter to `true` in your request. This returns not only the target symbol but also surrounding function and class definitions, providing broader context without requiring multiple separate queries.

### What happens if the qualified name does not exist?

The tool returns an error status indicating the symbol was not found. If the name is ambiguous (matches multiple nodes), you will receive an "ambiguous" status with a `snippet_suggestions` list containing possible alternatives, allowing you to select the correct qualified name and retry.