Skip to main content
Glama
maxkuminov

Obsidian MCP (pgvector + Ollama, self-hosted)

get_neighborhood

Retrieve the local cluster of notes connected by links and backlinks around a given seed note, up to a configurable depth. Ideal for summarizing everything linked to a project.

Instructions

The connected subgraph reachable from path via links or backlinks, up to depth hops (treated as undirected).

Use this when an agent needs the local cluster around a topic — e.g. "summarize everything connected to this project". Prefer this over find_related when explicit links are the signal you want; prefer find_related when the connection is conceptual rather than linked.

Args: path: Vault-relative path to the seed note. depth: Maximum BFS depth (default 1, capped at 5). limit: Maximum distinct neighbor notes (default 50, hard cap 200).

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
pathYes
depthNo
limitNo

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
resultYes

Implementation Reference

  • The actual implementation of get_neighborhood. BFS over the resolved-link graph treating links as undirected. Uses NoteLink table to find neighbors up to depth hops (capped at 5), with a limit on distinct notes (capped at 200). Returns formatted markdown output showing distance, title, file path, tags, and via path for each neighbor.
    @_tracked("get_neighborhood", ["path", "depth", "limit"])
    async def get_neighborhood_impl(path: str, depth: int = 1, limit: int = 50) -> str:
        """BFS over the resolved-link graph treating links as undirected."""
        from sqlalchemy import or_, select
        from src.models.db import NoteLink, NoteMetadata
    
        uid = current_user_id.get()
        depth = max(1, min(depth, 5))
        limit = max(1, min(limit, 200))
    
        async with async_session() as session:
            src_stmt = select(NoteMetadata).where(NoteMetadata.file_path == path)
            if uid is not None:
                src_stmt = src_stmt.where(NoteMetadata.user_id == uid)
            source = (await session.execute(src_stmt)).scalar_one_or_none()
            if source is None:
                return f"Note not found: {path}"
    
            # BFS state.
            seen: dict[int, dict] = {source.id: {"distance": 0, "via": None}}
            frontier: list[int] = [source.id]
            truncated = False
    
            for d in range(1, depth + 1):
                if not frontier:
                    break
                stmt = select(
                    NoteLink.source_note_id,
                    NoteLink.target_note_id,
                ).where(
                    or_(
                        NoteLink.source_note_id.in_(frontier),
                        NoteLink.target_note_id.in_(frontier),
                    ),
                    NoteLink.target_note_id.isnot(None),
                )
                edges = (await session.execute(stmt)).all()
                next_frontier: list[int] = []
                for src_id, tgt_id in edges:
                    # Walk both directions.
                    for from_id, to_id in ((src_id, tgt_id), (tgt_id, src_id)):
                        if from_id in seen and to_id not in seen:
                            seen[to_id] = {"distance": d, "via": from_id}
                            next_frontier.append(to_id)
                            if len(seen) - 1 >= limit:
                                truncated = True
                                break
                    if truncated:
                        break
                frontier = next_frontier
                if truncated:
                    break
    
            # Hydrate metadata for everything except the source. The BFS edges
            # were already scoped to this user's graph (indexer guarantees the
            # vault_index is per-user), but we filter again here as a defense
            # in depth so a corrupted state can't leak rows across users.
            ids = [nid for nid in seen if nid != source.id]
            if not ids:
                return f"`{path}` has no resolved-link neighbors"
            meta_stmt = select(NoteMetadata).where(NoteMetadata.id.in_(ids))
            if uid is not None:
                meta_stmt = meta_stmt.where(NoteMetadata.user_id == uid)
            meta_rows = (await session.execute(meta_stmt)).scalars().all()
            meta_by_id = {m.id: m for m in meta_rows}
            # Drop any ids that the user_id filter excluded (shouldn't happen
            # under normal operation but keeps the output consistent).
            ids = [i for i in ids if i in meta_by_id]
            if not ids:
                return f"`{path}` has no resolved-link neighbors"
            # We also need `via` paths — fetch those.
            via_ids = {seen[nid]["via"] for nid in ids if seen[nid]["via"] is not None}
            via_paths = {source.id: source.file_path}
            if via_ids - {source.id}:
                via_stmt = select(NoteMetadata.id, NoteMetadata.file_path).where(
                    NoteMetadata.id.in_(via_ids)
                )
                if uid is not None:
                    via_stmt = via_stmt.where(NoteMetadata.user_id == uid)
                via_rows = (await session.execute(via_stmt)).all()
                for vid, vpath in via_rows:
                    via_paths[vid] = vpath
    
        ordered = sorted(ids, key=lambda nid: (seen[nid]["distance"], meta_by_id[nid].file_path))
        lines = [
            f"Neighborhood of `{path}` (depth ≤ {depth}, {len(ordered)} notes"
            + (", truncated" if truncated else "") + "):\n"
        ]
        for nid in ordered:
            m = meta_by_id[nid]
            info = seen[nid]
            via_path = via_paths.get(info["via"], "?")
            tags_str = f" [{', '.join(m.tags)}]" if m.tags else ""
            lines.append(
                f"- d={info['distance']} **{m.title}** (`{m.file_path}`){tags_str} via `{via_path}`"
            )
        return "\n".join(lines)
  • MCP tool registration of get_neighborhood via @mcp.tool() decorator. Defines the tool signature (path, depth=1, limit=50) and delegates to get_neighborhood_impl.
    @mcp.tool()
    async def get_neighborhood(path: str, depth: int = 1, limit: int = 50) -> str:
        """The connected subgraph reachable from `path` via links or backlinks,
        up to `depth` hops (treated as undirected).
    
        Use this when an agent needs the local cluster around a topic — e.g.
        "summarize everything connected to this project". Prefer this over
        `find_related` when explicit links are the signal you want; prefer
        `find_related` when the connection is conceptual rather than linked.
    
        Args:
            path: Vault-relative path to the seed note.
            depth: Maximum BFS depth (default 1, capped at 5).
            limit: Maximum distinct neighbor notes (default 50, hard cap 200).
        """
        return await get_neighborhood_impl(path, depth=depth, limit=limit)
  • The _tracked decorator that wraps get_neighborhood_impl (and all other tools). Records timing, parameters, and response size to the usage_logs table.
    def _tracked(tool_name: str, param_keys: list[str]):
        """Decorator that times the call and logs it to usage_logs."""
        def decorator(fn):
            @wraps(fn)
            async def wrapper(*args, **kwargs):
                start = time.monotonic()
                result = await fn(*args, **kwargs)
                duration_ms = int((time.monotonic() - start) * 1000)
                params = {}
                for i, key in enumerate(param_keys):
                    if i < len(args):
                        params[key] = args[i]
                    elif key in kwargs:
                        params[key] = kwargs[key]
                await _log_usage(tool_name, _truncate_params(params), duration_ms, len(str(result)))
                return result
            return wrapper
        return decorator
  • Input schema/parameters defined via the @mcp.tool() function signature: path (str), depth (int, default 1), limit (int, default 50). The docstring describes behavior and constraints.
    @mcp.tool()
    async def get_neighborhood(path: str, depth: int = 1, limit: int = 50) -> str:
        """The connected subgraph reachable from `path` via links or backlinks,
        up to `depth` hops (treated as undirected).
    
        Use this when an agent needs the local cluster around a topic — e.g.
        "summarize everything connected to this project". Prefer this over
        `find_related` when explicit links are the signal you want; prefer
        `find_related` when the connection is conceptual rather than linked.
    
        Args:
            path: Vault-relative path to the seed note.
            depth: Maximum BFS depth (default 1, capped at 5).
            limit: Maximum distinct neighbor notes (default 50, hard cap 200).
        """
        return await get_neighborhood_impl(path, depth=depth, limit=limit)
Behavior4/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations provided, so description carries full burden. It explains BFS traversal, undirected nature, and caps (depth max 5, limit max 200). Minor omission: whether seed note itself is included, but overall strong.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

Concise and well-structured: one sentence for purpose, one for usage guidance, then bullet-like parameter list. No redundancy, every sentence earns its place.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given output schema exists, return values not needed. All key aspects covered: what, when, how. Could mention that path must exist, but not a major gap.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters5/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

With 0% schema description coverage, the description fully explains each parameter: path as vault-relative, depth with default 1 and max 5, limit with default 50 and max 200. This adds critical meaning beyond the schema.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly defines the tool as retrieving the connected subgraph reachable from a path via links/backlinks up to a depth, distinguishing it from siblings like get_links and get_backlinks by focusing on the undirected neighborhood.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines5/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

Explicit when-to-use statement with an example ('summarize everything connected to this project') and direct comparison to find_related, telling the agent when to prefer which tool.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Other Tools

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/maxkuminov/obsidian-mcp'

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