Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/aws-documentation-mcp-server/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- `search_table` searches every section whose heading matches `section_title`. Before, it read only the last one, so on a page that repeats a title it searched the wrong table: on the Lambda quotas page, `section_title="Compute and storage"` read only the Lambda MicroVMs table, and a query for "Concurrent executions" matched nothing. `read_sections` already returns every section that carries the title.
- `read_sections` no longer loses headings after code samples in converted HTML. `markdownify` opens a code fence after the `:` markers it writes for definition-list items (`: : ` followed by the fence), or after a list marker inside one (`: 1. `), and the heading parser only recognized fences at the start of a line. It missed the opening fence and took the indented closing fence for an opening one, so the headings that followed were treated as code. On IAM's condition operators page it found 10 of 21 headings, so titles from search failed with "No matching sections were found" and some sections ran on past headings it couldn't see. `truncate_large_tables` shared the check, so tables in that stretch were not truncated. A fence now also closes only on a fence of the same kind and at least the same length, with only indentation before it (at the opener's blockquote depth) and nothing after it but an optional anchor, and a backtick fence whose info string contains a backtick, such as ` ``` x ``` `, is inline code, not a fence.
- `read_sections` keeps content published in tabs, such as code samples and Console or AWS CLI procedures. Published Markdown starts each tab with a six-dash `------` rule and a `#### [ JSON ]` or `#### [ Console ]` line, which the parser read as a heading, so a section at that level or deeper ended before the tab's content. Those labels, including ones followed by an anchor, no longer end other sections or appear in the list of sections. A label can still be requested by name, which returns each tab that carries it.
- `read_sections` matches section titles at every heading level, not only `h2`. Search results list `sections` and `recommended_sections` from every level, including the page's `h1` title, so titles copied from them failed with "No matching sections were found" and agents fell back to `read_documentation`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ def parse_html_tables(html: str, section_title: Optional[str] = None) -> Optiona
Matches section_title against h1/h2/h3 headings (case-insensitive).
Collects all tables until the next heading at the same or higher level,
including tables under deeper nested headings (e.g., h4 under an h3).
When several headings carry the title, the tables of every one are collected.

Args:
html: Raw HTML content of the page
Expand All @@ -162,40 +163,41 @@ def parse_html_tables(html: str, section_title: Optional[str] = None) -> Optiona
return _find_all_tables(soup)

normalized_target = ' '.join(section_title.strip().lower().split())
section_element = None
section_elements: list[Tag] = []
available_sections = []

for heading in soup.find_all(['h1', 'h2', 'h3']):
heading_text = ' '.join(heading.get_text().split())
normalized = heading_text.lower()
available_sections.append(heading_text)
if normalized == normalized_target:
section_element = heading
if normalized == normalized_target and isinstance(heading, Tag):
section_elements.append(heading)

if not section_element:
if not section_elements:
return {
'error': f'Section "{section_title}" not found',
'available_sections': available_sections,
}

# Find ALL tables until the next heading at the same or higher level
if not isinstance(section_element, Tag):
return None
heading_level = int(section_element.name[1])
# Collect tables paired with the last sub-heading seen before them (within section)
# A title can head several sections (the Lambda quotas page has two "Compute and
# storage"), so search each one, as read_sections does. Find ALL tables until the
# next heading at the same or higher level, paired with the last sub-heading seen
# before them (within section)
tables: list[tuple[Tag, Optional[Tag]]] = []
last_sub_heading: Optional[Tag] = None
for sibling in section_element.find_next_siblings():
if isinstance(sibling, Tag):
if sibling.name in ['h1', 'h2', 'h3', 'h4', 'h5', 'h6']:
if int(sibling.name[1]) <= heading_level:
break
last_sub_heading = sibling
if sibling.name == 'table':
tables.append((sibling, last_sub_heading))
else:
for found in sibling.find_all('table'):
if isinstance(found, Tag):
seen: set[int] = set() # a matching heading can sit inside another matching section
for section_element in section_elements:
heading_level = int(section_element.name[1])
last_sub_heading: Optional[Tag] = None
for sibling in section_element.find_next_siblings():
if isinstance(sibling, Tag):
if sibling.name in ['h1', 'h2', 'h3', 'h4', 'h5', 'h6']:
if int(sibling.name[1]) <= heading_level:
break
last_sub_heading = sibling
found_tables = [sibling] if sibling.name == 'table' else sibling.find_all('table')
for found in found_tables:
if isinstance(found, Tag) and id(found) not in seen:
seen.add(id(found))
tables.append((found, last_sub_heading))

if not tables:
Expand Down
32 changes: 32 additions & 0 deletions src/aws-documentation-mcp-server/tests/test_table_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,38 @@ def test_section_with_multiple_tables(self):
assert result['tables'][0]['columns'] == ['Name', 'Default']
assert result['tables'][1]['columns'] == ['Name', 'Limit']

def test_repeated_section_title_searches_every_section(self):
"""A title that heads two sections returns the tables of both, in page order."""
html = """<html><body>
<h1>Lambda quotas</h1>
<h2>Compute and storage</h2>
<table><thead><tr><th>Resource</th></tr></thead>
<tbody><tr><td>Concurrent executions</td></tr></tbody></table>
<h2>Lambda MicroVMs</h2>
<h3>Compute and storage</h3>
<table><thead><tr><th>Resource</th></tr></thead>
<tbody><tr><td>Memory allocated across all MicroVMs</td></tr></tbody></table>
</body></html>"""
result = parse_html_tables(html, 'Compute and storage')
assert result is not None
assert [t['rows'][0]['Resource'] for t in result['tables']] == [
'Concurrent executions',
'Memory allocated across all MicroVMs',
]

def test_repeated_title_nested_in_matching_section_counts_tables_once(self):
"""A matching heading inside another matching section does not repeat its tables."""
html = """<html><body>
<h2>Quotas</h2>
<table><thead><tr><th>Name</th></tr></thead><tbody><tr><td>a</td></tr></tbody></table>
<h3>Quotas</h3>
<table><thead><tr><th>Name</th></tr></thead><tbody><tr><td>b</td></tr></tbody></table>
</body></html>"""
result = parse_html_tables(html, 'Quotas')
assert result is not None
assert [t['rows'][0]['Name'] for t in result['tables']] == ['a', 'b']
assert [t['table_heading'] for t in result['tables']] == [None, 'Quotas']

def test_section_with_one_table_returns_flat(self):
"""Single table in section returns flat format (no 'tables' key)."""
html = """<html><body>
Expand Down