liveBook · Manning

Tool

As explained in Build AI-Enhanced Web Apps

A tool is an externally invocable software capability that an AI workflow can call to perform an operation or obtain information.

A tool is a callable capability that lets an AI workflow do more than generate text. It can fetch information, query a database, access files, or call an API. An agent uses a model to decide when to invoke a tool and can incorporate the result into its response. section 12.1 section 6.4.2

In LangChain.js, an agent combines a model, a system prompt, and a list of tools. Tools can come from community integrations or be defined with `tool` from `@langchain/core/tools`. The agent can then participate in a larger chain. section 6.4.2

Model Context Protocol (MCP) offers another way to expose tools. An MCP server registers callable tools alongside prompts and resources; a client discovers and invokes them through a shared protocol. This lets applications reuse capabilities without wiring every external service directly into the assistant. section 12.0 section 12.4

For a local Next.js integration:

  • Register the tool on an MCP server with a name, description, and implementation that returns content. Handle failed API responses and exceptions so the caller receives a predictable result. section 12.4.1
  • Have the Next.js API route start an MCP client over stdio, obtain the server’s tools, and pass them to the Vercel AI SDK’s `streamText` call. section 12.3.2
  • Keep external API calls in the MCP server rather than the model-facing route. The model selects an exposed tool; the server performs the request and returns its output. section 12.3.2 section 12.3.3
Listing 12.4 Registering a tool in an MCP server
server.tool(    #1
  "get-chuck-joke",
  "Fetch a random Chuck Norris joke",
  {},
  async () => {
    try {
      const response = await fetch("https://api.chucknorris.io/jokes/random");
      if (!response.ok) {
        return {
          content: [{ type: "text", text: `No joke available 
at the moment.` }],
        };
      }
      const data = await response.json();
      return { content: 
[{ type: "text", text: data.value }] };    #2
    } catch (err) {
      const message = 
err instanceof Error ? err.message : "An unknown error occurred";
      return { content: 
[{ type: "text", text: `Error fetching joke: ${message}` }] };
    }
  }
);

The tool registration shows an API-backed implementation, a text-content return value, and fallback responses for request failures. section 12.4.1

Listing 12.2 API route that serves the MCP server in Next.js
import { createGoogleGenerativeAI } from '@ai-sdk/google';
import { streamText, convertToModelMessages, 
experimental_createMCPClient } from 'ai';
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio';

export const maxDuration = 30;

const gemini = createGoogleGenerativeAI({
    apiKey: process.env.GEMINI_API_KEY || '',
});

export async function POST(req) {
    const { messages } = await req.json();
    const transport = new StdioClientTransport({    #1
        command: 'node',
        args: ['src/stdio/server.js'],
    });

    const mcpClient = 
await experimental_createMCPClient({ transport });    #2
    const tools = await mcpClient.tools();    #3

    const result = streamText({
        model: gemini('gemini-2.5-flash'),
        messages: convertToModelMessages(messages),
        tools,
        system: 'You are a helpful assistant that can 
call tools when needed.',
        onFinish: async () => {
            await mcpClient.close();
        },
        onError: async () => {
            await mcpClient.close();
        },
    });
    return result.toUIMessageStreamResponse();    #4
}

The API route connects to the local MCP server, retrieves its tools, and supplies them to the model call. section 12.3.2

The same MCP tools can also reach a LangChain.js agent through `MultiServerMCPClient`, which exposes them as LangChain tools. Use the MCP Inspector to inspect and test registered capabilities while developing the server. section 12.5.1 section 12.4.1

How Build AI-Enhanced Web Apps covers it

How deep it goes
Works through tool registration, client discovery, invocation, API-backed results, and failure handling in a local MCP integration.
Examples
Uses Wikipedia search in a LangChain.js agent and an API-backed joke tool in a Next.js chat application.
What it assumes
Introduces MCP server and tool registration step by step; running the example requires a Gemini or OpenAI API key.
What you can do after
Build it from scratch as an MCP server tool, connect it to a Next.js assistant, and test it with MCP Inspector.
How it connects
Connects agents, system prompts, MCP servers, MCP clients, resources, and external APIs.

Also explained in

Compare how 14 books explain tool →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build AI-Enhanced Web Apps. The section links point to the parts of the book it draws on.