Summary audio
Spoken summary — press play to read along: the line being spoken stays near the top.
Study notes
MCP Resources: Document Mention Feature
Goal: Implement a feature allowing users to mention documents (e. g. , @report. pdf) to automatically fetch and insert the document's content into the prompt sent to the LLM (Claude).
Core Components:
- Two Features:
- Autocomplete: When the user types @, the MCP server must provide a list of all available document names.
- Content Fetching: When the user submits the message, the MCP server must retrieve the content of the mentioned document.
- Mechanism: The MCP server uses Resources to expose specific data and read operations to the client.
Resource Definitions:
- Purpose: Define one resource for each distinct read operation.
- Required Resources:
- A resource to return a list of all document names (for autocomplete).
- A resource to return the content of a single document based on its ID.
Resource Types:
- Direct (Static) Resources:
- Have a fixed, unchanging URI (e. g. , docs://documents).
- Used for operations that do not require user input parameters.
- Templated Resources:
- Contain one or more parameters/wildcards in the URI (e. g. , docs://documents/{doc_id}).
- The MCP SDK automatically parses these parameters and passes them as keyword arguments to the associated function.
- Used when customization or selection is needed (e. g. , fetching a specific document).
Client-Server Interaction Flow (Autocomplete Example):
- User types @ in the client.
- Client → MCP Client sends a Read Resource Request.
- The request includes the specific URI defined for the resource.
- MCP Server uses the URI to locate and execute the corresponding function.
- Server returns a Read Resource Result message to the client.
- Client displays the returned data (e. g. , the list of document names) in the autocomplete window.
Implementation Details (Code Structure):
- List Documents Resource (Static):
- URI: docs://documents
- MIME Type: application/json (Indicates structured data).
- Function returns: A list of strings (document IDs/names).
- Fetch Document Resource (Templated):
- URI: docs://documents/{doc_id}
- MIME Type: text/plain (Indicates raw, unstructured text content).
- Function receives: doc_id as a keyword argument.
- Function returns: The string content of the requested document.
Takeaways
- The document mention feature relies on MCP Resources to perform two core functions: providing a list of available document names (autocomplete) and retrieving the content of a specific document.
- Resources are defined as either Direct (Static) or Templated, where Templated resources use parameters (like {doc_id}) to allow for dynamic data retrieval.
- The client initiates the process by sending a Read Resource Request containing the specific URI to the MCP server, which then executes the corresponding function.
- A static resource (e. g. , docs://documents) is used to return a list of all document IDs, while a templated resource (e. g. , docs://documents/{doc_id}) is used to fetch the raw text content of a single document.
Flashcards 8 cards
Question
click to reveal · ←/→
Answer
click to flip back
Knowledge check 6 questions