Claude With Amazon Bedrock
← Toutes les leçons
Leçon 57Claude With Amazon Bedrock

Defining tools with MCP

Audio récapitulatif

Pas de récapitulatif audio pour cette leçon.

Notes de cours

🧠 Notes: Building an MCP Server with Tools

**I. Overview of the MCP Server**

  • Purpose: To add functionality (tools) to the existing CLI chat bot.
  • Location: The server implementation resides in MCP server. py within the root project directory.
  • Initial Setup: The server starts with an in-memory collection of documents (stored in a dictionary, docs).

**II. Using the MCP Python SDK**

  • Benefit: The official MCP Python SDK simplifies server creation and tool definition significantly.
  • Server Creation: The SDK allows for the creation of the MCP server with minimal code (one line).
  • Tool Definition: Instead of manually writing complex JSON schemas, the SDK allows tools to be defined using standard Python syntax.
  • Process: When a tool is defined in Python, the MCP SDK automatically generates the necessary tool JSON schema for use with Bedrock.

**III. Implementing Tools (General Procedure)**

  • Goal: Define a tool's name, description, required arguments, and the actual function logic.
  • Tool Definition Structure:
  • Use MCP. tool to define the tool.
  • Provide a clear name and description.
  • Define the function that executes the tool's logic.
  • Argument Handling: Arguments are defined using field from Pydantic (e. g. , field(description="... ")).

**IV. Tool Implementation Examples**

**A. Tool 1: Reading Document Contents (read_doc_contents)**

  • Goal: Take a document ID and return its contents.
  • Logic:
  • Check if the provided doc_id exists as a key in the docs dictionary.
  • If not found, raise a ValueError (e. g. , "doc with ID [doc_id] not found").
  • If found, return the corresponding document content (docs[doc_id]).

**B. Tool 2: Editing Document Contents (edit_document)**

  • Goal: Perform a find-and-replace operation within a document.
  • Arguments Required:
  • doc_id (string): ID of the document to edit.
  • old_string (string): The exact text to find (including whitespace).
  • new_string (string): The text to replace the old string with.
  • Logic:
  • Check if the doc_id exists in the docs dictionary.
  • If found, execute the replacement: docs[doc_id] = docs[doc_id]. replace(old_string, new_string).
  • Handle the case where the document ID is not found by raising a ValueError.

Takeaways

  • Le serveur MCP sert à ajouter des fonctionnalités (outils) au chatbot CLI et est implémenté dans MCP server. py.
  • Le SDK Python MCP simplifie la création de serveurs et de définitions d'outils en utilisant la syntaxe Python au lieu de schémas JSON complexes.
  • Pour définir un outil, on utilise MCP. tool en fournissant un nom, une description et la logique de fonction.
  • La gestion des arguments des outils est assurée par field de Pydantic.
  • Les outils manipulent une collection de documents en mémoire (docs) pour lire ou modifier le contenu.
Cartes mémo 8 cartes
Question
cliquez pour révéler · ←/→
Réponse
cliquez pour retourner
Exporter vers Anki (.tsv) ↓
Vérification des connaissances 0 questions

No quiz for this lesson yet.