# MCP for developers: Result schema, IDs and retries for Shamela MCP tools

Source: https://qurangallery.com/docs/mcp/result-schema-and-ids

> What each Shamela MCP tool returns, how book and page IDs work, whether results are safe to retry, and how to validate structured JSON output.

Every Shamela MCP tool returns a machine-readable tool result: a `structuredContent` object that its published JSON schema (`outputSchema`) describes, plus the same JSON as text. This page lists the fields each tool returns, explains the book and page IDs in a result, and says what a client can rely on when it retries a call. The server publishes no stability promise for IDs; what follows was observed on 2026-12-02.

The server serves classical Arabic books from the Shamela library with a citation to the printed volume and page where the book has one. It also looks up Quran verses in Arabic (Hafs from Asim) and the tafsir on a verse (`shamela_lookup_quran`, `shamela_lookup_tafsir`). It does not serve Quran translations, grade hadith or give rulings.

Search matches the words of the query and ignores diacritics and common spelling variants; root expansion is an option. It is not semantic or vector search.

## What a tool result contains

Every tool call returns an MCP result with three parts:

| Part               | Type    | Meaning                                                                                       |
| ------------------ | ------- | --------------------------------------------------------------------------------------------- |
| `structuredContent` | object  | The machine-readable result. Its shape is the tool's `outputSchema`.                          |
| `content`          | array   | The same result as JSON text, and sometimes a short readable summary.                         |
| `isError`          | boolean | `true` for a tool error such as an invalid scope or a query with no Arabic words; omitted on success. |

All 15 tools publish an `outputSchema` in `tools/list`. The same listing marks every tool `readOnlyHint: true`, `idempotentHint: true` and `destructiveHint: false`; the [retry section](#safe-to-retry) uses those annotations.

The tables below name the top-level fields of `structuredContent` and, where the schema names them, the fields of one item in the main array. A tool may add a field for one mode only; the schema marks it with that mode. An array whose items are an open object is not expanded here: read its fields from the response description in `tools/list` or from the JSON itself.

## Search tools

### `shamela_search_library`

| Field             | Type            | Meaning                                                                 |
| ----------------- | --------------- | ----------------------------------------------------------------------- |
| `passages`        | array           | Passages, best evidence first; empty when nothing in the scope matched. |
| `relaxedToTokens` | boolean         | `true` when a looser word search answered instead of the exact phrase.  |
| `scope`           | string          | The scope searched, as sent.                                            |
| `booksSearched`   | integer or null | Books in that scope, or `null` when the library did not say.            |
| `totalMatches`    | integer or null | Matching pages before the limit, or `null` on the fallback pass.        |
| `note`            | string          | A caveat when the result needs one; carry it with the passages.         |

Each item in `passages`:

| Field          | Type            | Meaning                                                                   |
| -------------- | --------------- | ------------------------------------------------------------------------- |
| `book`         | string          | Title including the printing.                                             |
| `author`       | string          | Author name.                                                              |
| `authorDied`   | integer or null | Hijri death year, or `null`.                                              |
| `bookId`       | integer         | Shamela's book id.                                                        |
| `printedPage`  | string or null  | Volume and page as that printing paginates it, or `null`.                 |
| `pageId`       | integer         | Shamela's page id.                                                        |
| `arabic`       | string          | The passage, verbatim, markup removed; at most 1,600 characters.          |
| `matchedIn`    | string          | `body`, `footnote`, `both` or `unknown`.                                  |
| `matchQuality` | string          | How the library matched, for example `exact_phrase`.                      |
| `edition`      | string          | The printing's label; omitted when the library records none.              |
| `editor`       | string          | The editor; omitted when the library records none.                        |
| `publisher`    | string          | The publisher; omitted when the library records none.                     |
| `sourceUrl`    | string          | The same page in the Quran Gallery Maktabah reader.                       |

### `search`

`search` returns one field, `results`: up to 10 pages, empty when no page carries the words. Each item has `id` (string, the `bookId:pageId` form), `title` (string, book, author and volume/page) and `url` (string, the Maktabah reader page).

### `shamela_search`

| Field              | Type    | Meaning                                                              |
| ------------------ | ------- | -------------------------------------------------------------------- |
| `total_hits`       | number  | Every match in scope, not just the rows returned.                    |
| `returned`         | number  | Rows in `results` on this call.                                      |
| `offset`           | number  | Offset this page started at.                                         |
| `has_more`         | boolean | True when another page exists.                                       |
| `next_offset`      | number  | Offset of the next page; absent on the last page.                    |
| `query`            | string  | The query as received.                                               |
| `normalized_tokens` | array  | The query after Arabic folding, as searched.                         |
| `scope_count`      | number  | Books in the resolved scope; `-1` when unscoped.                     |
| `deadline_hit`     | boolean | True when a time limit stopped the call; its zero is not a finding.  |
| `results`          | array   | Matching pages; the schema for this build does not name their fields. |
| `truncated`        | boolean | True when rows were dropped.                                         |
| `notes`            | array   | Caveats about how the call was answered.                             |

The schema also names mode-specific fields such as `paging_ceiling`, `zero_hit_terms`, `respelled_terms`, `coverage`, `probes` and `edition_groups`; they are absent when they do not apply.

### `shamela_find`

| Field                | Type            | Meaning                                                                     |
| -------------------- | --------------- | --------------------------------------------------------------------------- |
| `status`             | string or null  | `exact`, `ambiguous` or `not_found` for a work lookup.                      |
| `match`              | object          | The single catalogue entry when `status` is `exact`.                        |
| `candidates`         | array           | Entries carrying the requested title.                                       |
| `available_editions` | array           | Printings that exist when the requested edition does not.                   |
| `reason`             | string or null  | `no_title_match`, `author_mismatch` or `edition_not_found`.                 |
| `results`            | array           | Book, heading or author rows, depending on `kind`.                          |
| `total_hits`         | number          | Every match in scope.                                                       |
| `notes`              | array           | Guidance on reading the result.                                             |

The `candidates`, `available_editions` and `same_author_books` items name `book_id`, `book_name`, `work`, `edition`, `author_id`, `author_name`, `work_group_id`, `book_date`, `citable` and `downloaded`.

### `shamela_survey_topic`

| Field           | Type    | Meaning                                                          |
| --------------- | ------- | ---------------------------------------------------------------- |
| `total_hits`    | integer | Exact number of matching pages.                                  |
| `total_counted` | integer | Hits folded into the distribution.                               |
| `books_matched` | integer | Books with at least one match.                                   |
| `by_category`   | array   | `name` and `count`, top 15 categories.                           |
| `by_book`       | array   | `book_id`, `book_name`, `author_name` and `count`, top 15 books. |
| `accuracy_note` | string  | The accuracy caveat for a morphological count.                   |

### `shamela_read`

| Field              | Type            | Meaning                                                               |
| ------------------ | --------------- | --------------------------------------------------------------------- |
| `book_id`          | integer or null | Catalogue book id.                                                    |
| `book_name`        | string or null  | Catalogue title.                                                      |
| `page_id`          | integer or null | Page id that was read.                                                |
| `printed_page`     | string or null  | Printed page label as Shamela renders it.                             |
| `part`             | string or null  | Volume label, or `null` for a single-volume book.                     |
| `body`             | string or null  | The main text (matn), or one part of it.                              |
| `foot`             | string or null  | Footnotes, the editor's words.                                        |
| `comment`          | string or null  | The user annotation stored with the page; usually empty.              |
| `body_part`        | number or null  | Which part of the body this is.                                       |
| `body_total_parts` | number or null  | How many parts the body has.                                          |
| `body_has_more`    | boolean or null | True when another body part follows.                                  |
| `prev_page_id`     | number or null  | Previous existing page id, because ids have gaps.                     |
| `next_page_id`     | number or null  | Next existing page id.                                                |
| `containing_titles` | array          | `title_id`, `title_text` and `page_id` for each enclosing heading.    |
| `pages`            | array           | For a range or section: `page_id`, `printed_page`, `part`, `body`, `foot` and `comment`. |

## Citation and verification

### `shamela_verify_quote`

| Field      | Type   | Meaning                                                                        |
| ---------- | ------ | ------------------------------------------------------------------------------ |
| `verdict`  | string | `found` or `not_found`; `not_found` applies to the searched scope only.        |
| `matches`  | array  | Up to the limit, best first; empty when `not_found`.                           |
| `searched` | object | The scope, book id or whole-library flag that was searched.                    |
| `elsewhere` | array | Up to three pages in other books with the same words, after a `not_found`.     |
| `closest`  | object | After a `not_found`, the page sharing most of the quoted words; never a match. |
| `note`     | string | Caveats to carry with the verdict.                                             |

Each item in `matches` names `docId` (`bookId:pageId`), `bookId`, `pageId`, `book`, `author`, `volume`, `printedPage`, `edition`, `matchedIn`, `exact`, `differences`, `snippet` and `sourceUrl`.

### `shamela_cite`

| Field            | Type            | Meaning                                                                  |
| ---------------- | --------------- | ------------------------------------------------------------------------ |
| `formatted`      | string or null  | The citation in the requested style.                                     |
| `style`          | string or null  | `shamela`, `short`, `full`, `bibtex` or `ris`.                           |
| `page_resolved`  | boolean or null | True when the page and printed number were found.                        |
| `components`     | object          | The parts the citation was built from, including `auto_numbered`.        |
| `notes`          | array           | Missing citation fields and fallbacks; never fill them in.               |
| `status`         | string or null  | `ok`, `book_not_downloaded`, `no_printed_pagination`, `part_not_found` or `page_not_in_part`. |
| `book_id`        | integer or null | Book id for a printed-page lookup.                                       |
| `page_ids`       | array           | Every page id carrying the printed page, in reading order.               |
| `matches`        | array           | `page_id`, `part`, `page` and `printed_page` per page id.                |

## Hadith and narrators

### `shamela_lookup_hadith`

| Field                | Type    | Meaning                                                               |
| -------------------- | ------- | --------------------------------------------------------------------- |
| `query`              | string  | The query as received.                                                |
| `total_text_matches` | number  | Every page matching the text in the searched shelves.                 |
| `books_searched`     | number  | Books the text search covered.                                        |
| `searched`           | string  | `hadith_shelves` or `whole_library`.                                  |
| `matches`            | array   | Matching pages, key-bearing first.                                    |
| `takhrij`            | array   | Per hadith key: `hadith_key`, `index_entries` and `books`.            |
| `results`            | array   | For a sources lookup: `book_id`, `book_name`, `author_name`, `page_id` and `downloaded`. |

### `shamela_lookup_rijal`

| Field               | Type            | Meaning                                                           |
| ------------------- | --------------- | ----------------------------------------------------------------- |
| `query`             | string          | The name searched.                                                |
| `total_matches`     | integer         | Narrators matching, before paging.                                |
| `returned`          | integer         | Rows in this response.                                            |
| `results`           | array           | Narrators or chains, depending on the mode.                       |
| `narrator_id`       | integer         | Narrator id for one narrator.                                     |
| `name`              | string          | Short name.                                                       |
| `full_name`         | string          | Full name.                                                        |
| `death_year`        | number or null  | Hijri death year.                                                 |
| `grade_ibn_hajar`   | string or null  | The grading attributed to Ibn Hajar, when the register has one.    |
| `grade_dhahabi`     | string or null  | The grading attributed to al-Dhahabi, when the register has one.   |
| `tabaqa`            | string or null  | Generation, per Taqrib al-Tahdhib.                                |
| `jarh_tadil`        | array           | Critics' verdicts, grouped by critic, each with book and page.    |
| `note`              | string or null  | A note on the narrator record.                                    |

## Quran and tafsir

### `shamela_lookup_quran`

| Field          | Type    | Meaning                                                              |
| -------------- | ------- | -------------------------------------------------------------------- |
| `total_hits`   | number  | Every match in scope.                                                |
| `returned`     | number  | Rows in this response.                                               |
| `query`        | string  | The query as received.                                               |
| `results`      | array   | Verses in mushaf order; the schema for this build does not name their fields. |
| `aya_id`       | integer | Cumulative verse id, 1 to 6236, for an `aya` lookup.                 |
| `surah`        | integer | Surah number, 1 to 114.                                              |
| `surah_name`   | string  | Arabic surah name.                                                   |
| `aya`          | integer | Verse number within the surah.                                       |
| `body`         | string  | The verse in standard Egyptian spelling, Hafs from Asim.             |
| `verses`       | array   | `aya_id`, `surah`, `surah_name`, `aya`, `body`, `amiri` and `majma`. |

### `shamela_lookup_tafsir`

| Field          | Type    | Meaning                                                          |
| -------------- | ------- | ---------------------------------------------------------------- |
| `aya_id`       | integer | Verse id, 1 to 6236.                                             |
| `surah`        | integer | Surah number.                                                    |
| `aya`          | integer | Verse number within the surah.                                   |
| `totals`       | object  | Count of books in each coverage status.                          |
| `books`        | array   | One row per tafsir book, with the coverage status.               |
| `sources`      | array   | The commentary texts read for a `texts` lookup.                  |
| `remaining_book_ids` | array | Indexed sources not read in this call.                          |

Each item in `books` names `book_id`, `book_name`, `author_name`, `downloaded`, `status`, `page_id` and `printed_page`. Each item in `sources` names `book_id`, `book_name`, `status`, `page_id`, `printed_page`, `text`, `foot`, `text_has_more` and `note`.

## Catalogue and server brief

### `shamela_get_details`

| Field             | Type            | Meaning                                                              |
| ----------------- | --------------- | -------------------------------------------------------------------- |
| `book_id`         | integer or null | Catalogue book id.                                                   |
| `book_name`       | string or null  | Catalogue title.                                                     |
| `category`        | string or null  | Category name.                                                       |
| `book_type_label` | string or null  | Book type in Arabic, such as printed or manuscript.                  |
| `book_date`       | number or null  | Shamela's Hijri book date, usually the author's death year.          |
| `printed`         | number or null  | `1` when page numbers follow a printed edition.                      |
| `downloaded`      | boolean or null | True when the text is installed on this server.                      |
| `content_status`  | string or null  | `readable`, `downloaded_no_pages` or `not_downloaded`.               |
| `authors`         | array           | `author_id`, `author_name`, `death_year` and `role`.                 |
| `edition`         | string or null  | Edition from the front matter; never invented.                       |
| `editor`          | string or null  | Editor from the front matter.                                        |
| `publisher`       | string or null  | Publisher from the front matter.                                     |
| `same_group`      | array           | Other entries of the same work: `book_id`, `book_name`, `downloaded` and `printed`. |
| `author_id`       | integer or null | Author id for an author lookup.                                      |
| `author_name`     | string or null  | Author name for an author lookup.                                    |
| `books`           | array           | The author's books: `book_id`, `book_name`, `book_date` and `downloaded`. |

### `shamela_get_brief`

| Field           | Type   | Meaning                                                              |
| --------------- | ------ | -------------------------------------------------------------------- |
| `server_version` | string | This build's version.                                                |
| `capabilities`  | array  | Arguments and response fields this build supports.                   |
| `brief_hash`    | string | Digest of the other fields; re-fetch only when it changes.           |
| `limits`        | object | Numeric server limits by name.                                       |
| `tool_aliases`  | object | Retired tool names mapped to their replacement.                      |
| `traps`         | array  | `id`, `tools`, `risk`, `detect` and `act` for each known trap.       |

## Identifiers

A result uses three identifier forms:

| Form              | Where it appears                                      | Meaning                                      |
| ----------------- | ----------------------------------------------------- | -------------------------------------------- |
| `bookId` or `book_id` | Page, passage and catalogue results                | Shamela's catalogue id for one printing.     |
| `pageId` or `page_id` | Page and passage results                           | Shamela's id for one page record.            |
| `bookId:pageId`   | `search` results, `shamela_verify_quote` `docId`      | The same pair in one string, ready for `fetch`. |

A live `shamela_search_library` result on 2026-12-02 carried this entry:

```json
{
	"book": "صحيح البخاري - ط السلطانية",
	"bookId": 1681,
	"pageId": 10,
	"printedPage": "1/ 6",
	"sourceUrl": "https://qurangallery.com/maktabah/book/1681/00#p10"
}
```

The pair `1681:10` addresses that page for `fetch`; `shamela_read` takes `book_id: 1681` and `page_id: 10` separately. The `printedPage` value is the printing's own label, while `pageId` is Shamela's internal record. Where a book has no printed pagination, `printedPage` is `null` and the page id is Shamela's own auto-numbered address, not a printing's page; [Edition, editor and publisher](https://qurangallery.com/docs/mcp/edition-metadata) explains how to read and cite that difference.

The IDs are identifiers of convenience, not a contract. As observed on 2026-12-02, a book id and page id stayed the same across calls. No stability guarantee is published; keep in mind an id may change without notice. Keep the printed citation next to the id, so a reader can still find the page if the number changes. `shamela_get_brief` returns a `brief_hash`; check it when you need to know whether the server's capabilities changed.

## Safe to retry

Every tool in the live list is annotated `readOnlyHint: true` and `idempotentHint: true`, with `destructiveHint: false`. A retry cannot write anything, and repeating the same call returns the same result unless the catalogue changed between calls. That makes a retry after a network failure safe; it does not make a different query return the first query's answer.

A refused call spends nothing. If the per-minute limit refuses a call, the daily count is untouched ([Rate limits and error codes](https://qurangallery.com/docs/mcp/limits-and-errors)).

A rate-limit refusal is HTTP 429 with a JSON-RPC error whose code is `-32004` and whose `id` is `null`. The `retry-after` header gives the number of seconds to wait. For the keyless per-minute limit the message names the limit and suggests a free key:

> _Keyless limit reached: 10 tool calls a minute per IP. A free API key raises it to 60 a minute: …_

Wait for `retry-after`, then retry once. If the call still fails, stop and report the limit rather than sending a loop.

A tool error arrives as a normal result with `isError: true`; retrying it unchanged fails the same way. Fix the argument first, for example by sending a valid scope.

No timeout value for a whole tool call is published. In the code, each request to the library server has a 15-second internal limit (`apps/mcp/src/lib/server/shamela/client.ts:13`), but one tool call can make several such requests. Set your own client-side timeout and treat an expired call as retryable.

## Validate a result

The `outputSchema` in `tools/list` is a JSON Schema, so the `jsonschema` package can check a saved `structuredContent` against it (`pip install jsonschema`, preferably in a virtual environment):

```python
import json
from jsonschema import Draft202012Validator

schema = json.load(open("schema.json"))  # the tool's outputSchema from tools/list
result = json.load(open("result.json"))  # a saved structuredContent

errors = list(Draft202012Validator(schema).iter_errors(result))
print("valid" if not errors else f"{len(errors)} errors")
for error in errors:
    print(error.message)
```

Run against the saved `shamela_search_library` reply from 2026-12-02 with that tool's `outputSchema`, it printed:

```text
valid
```

The schemas in this build publish no `required` list, so validation checks types and structure, not that a field is present.

## Next steps

- [Open Quran Gallery MCP](https://qurangallery.com/mcp)
- [MCP tools for classical Arabic books: the full list](https://qurangallery.com/docs/mcp/tools)
- [Edition, editor and publisher in Shamela MCP results](https://qurangallery.com/docs/mcp/edition-metadata)
- [MCP rate limits and error codes](https://qurangallery.com/docs/mcp/limits-and-errors)
