ComenzarEmpieza gratis

Search for a Book by Title with the Open Library MCP

The Open Library MCP exposes tools that query the Internet Archive's Open Library API. In this exercise you will connect to the server, initialize a session, and use the get_book_by_title tool to search for a book and print the result.

Because of the unique setup of DataCamp exercises, the command and args set in StdioServerParameters() differ from those shown in the video and what you would use locally, but the principle is the same: the server is being spun up from the installed source files.

Este ejercicio forma parte del curso

Introduction to Model Context Protocol (MCP)

Ver curso

Instrucciones del ejercicio

  • Call the get_book_by_title tool to search for book titles containing "AI" and assign the result to result.
  • Assign the result text from result.content[0].text to text and print it.

Ejercicio interactivo práctico

Prueba este ejercicio y completa el código de muestra.

from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client

async def main():
    params = StdioServerParameters(command=OPEN_LIBRARY_SERVER_CMD, args=OPEN_LIBRARY_SERVER_ARGS)
    async with stdio_client(params) as (reader, writer):
        async with ClientSession(reader, writer) as session:
            await session.initialize()
            # Call get_book_by_title for "AI" and assign the result
            result = await session.call_tool(____, {"title": "____"})
            # Assign the result text and print it
            text = result.content[0].____
            print(text)

asyncio.run(main())
Editar y ejecutar código