sdk-python icon indicating copy to clipboard operation
sdk-python copied to clipboard

[BUG] strands-mcp: search never indexes document content (title-only search)

Open yonib05 opened this issue 4 months ago • 2 comments

Checks

  • [x] I have updated to the lastest minor and patch version of Strands
  • [x] I have checked the documentation and this is not expected behavior
  • [x] I have searched ./issues and there are no duplicates of my issue

SDK Language

Python

Strands Version

strands-mcp @ main (as merged via #3300)

Language Runtime Version

Python 3.10+ (any)

Operating System

Any

Installation Method

other

Steps to Reproduce

  1. Load the index and run a search for a term that appears in document bodies but not titles:
from strands_mcp_server.utils import cache
cache.ensure_ready()
idx = cache.get_index()
print(idx.search("guardrail"))      # []
print(idx.search("temperature"))    # []
print(len(idx.doc_indices))         # 831 distinct tokens for 763 docs
  1. Hydrate a page and check the indexed doc:
url = "https://strandsagents.com/docs/user-guide/concepts/model-providers/amazon-bedrock/index.md"
cache.ensure_page(url)              # fetches 49,700 chars
doc = next(d for d in idx.docs if d.uri == url)
print(len(doc.content))             # 0 - index never updated

Expected Behavior

search_docs searches document content, as its docstring claims ("the complete Strands Agents documentation"). Queries for terms that appear in doc bodies (e.g. "guardrail", "temperature") return relevant results.

Actual Behavior

Search is title-only. Documents are indexed at startup with content="" (cache.py, load_links_only), and ensure_page() writes fetched content to _URL_CACHE but never updates the indexed Doc or the inverted index. Body-only queries return zero results, and the consuming agent concludes the docs do not cover the topic. All Markdown-aware scoring in indexer.py (headers 4x, code blocks 2x, link text 2x, body TF) is dead code that always runs against empty content.

Additional Context

An empty result is worse than a bad result here: the LLM consuming this tool trusts the empty result and reports the topic as undocumented. This is the highest-priority item in the tracking issue; most other search-quality improvements matter less until this is fixed.

Possible Solution

Options in increasing effort:

  1. Update the index when a page is hydrated via ensure_page()
  2. Background-prefetch all pages at startup and build a full-text index (763 markdown files is small)
  3. Add a persisted disk cache so the prefetch is only paid once (tracked separately)

Once body content is indexed, consider BM25 instead of raw TF-IDF: without length normalization, TF will heavily favor the longest API pages. BM25 is ~30 lines with no dependencies, preserving the stdlib-only property.

Related Issues

#3317

yonib05 avatar Jul 17 '26 14:07 yonib05

I'd like to take this one. Proposed approach, in line with the options you listed:

  1. Index on hydration (your option 1) as the core fix: ensure_page() updates the indexed Doc and the inverted index when content is fetched, so the existing Markdown-aware scoring (headers 4×, code blocks 2×, link text 2×, body TF) stops being dead code.
  2. Background prefetch (option 2) behind a flag: 763 files is small enough to prefetch at startup, but I'd keep it opt-in until the disk cache (#3331) lands so cold starts don't pay the fetch every run.
  3. BM25 with title boost replacing raw TF-IDF, as you suggested — ~30 lines, stdlib-only preserved, with length normalization so long API pages don't dominate. Title matches keep priority so current behavior stays dominant for exact title queries.
  4. Tests: relevance fixtures asserting body-only terms ("guardrail", "temperature") return results post-hydration, plus a ranking regression suite.

Does this match the direction you had in mind under #3317? Happy to adjust scope — and I can sequence #3331 (disk cache) as a follow-up since it pairs naturally.

dosvk avatar Jul 18 '26 02:07 dosvk

Update after #3502: content is now indexed when a page is hydrated, but only pages that have already been fetched in the session are searchable by body text, so first-call searches are still title-only.

A direct way to index everything at startup: https://strandsagents.com/llms-full.txt (about 3.9 MB) contains all 344 prose pages (user guide, integrations, learning, blog), each terminated by a Source: <url> line followed by ---. One fetch, split on Source:, gives full content for every guide page, which can be indexed immediately and used to pre-populate the URL cache. API reference pages are not in that file and can stay lazily hydrated.

This pairs with #3331 (persist the parsed index to disk) and #5860 (load lazily so startup does not depend on the network).

yonib05 avatar Nov 05 '26 18:11 yonib05