Dev.to AI 🤖 Ai 👁 0 📖 5 min read

Day 29/90 — Building a Read-Only MCP Server: A Practical Approach to Safer AI Agents

One of the challenges in building AI agents is connecting them to real-world systems without giving them unnecessary control. An LLM can understand a user's request and decide which information it needs, but accessing t

One of the challenges in building AI agents is connecting them to real-world systems without giving them unnecessary control.

An LLM can understand a user's request and decide which information it needs, but accessing that information often requires integration with databases, APIs, or external services.

Traditionally, developers build custom integrations for each system. This approach works, but as the number of tools grows, managing integrations becomes increasingly complex.

This is one of the problems the Model Context Protocol (MCP) is designed to address.

MCP provides a standardized way for AI applications to discover and interact with external capabilities. However, standardizing communication is only part of the challenge. We also need to control what those capabilities are allowed to do.

A useful starting point is building a read-only MCP server.

Understanding the Architecture

Consider a document support assistant that helps users retrieve company policies, search technical documentation, and check support ticket statuses.

The assistant needs access to information stored in different systems, but it does not necessarily need permission to modify anything.

Instead of providing unrestricted database access, we can introduce an MCP server that exposes a limited set of tools.

For example:

  • search_documents(query) — Searches indexed documents and returns relevant results.
  • get_document(document_id) — Retrieves an existing document.
  • get_ticket_status(ticket_id) — Returns the current status of a support ticket.

These tools provide useful information without intentionally performing write operations.

The communication follows this architecture:

AI Application → MCP Client → MCP Server → Backend Services or Database

The MCP client communicates with the server using the protocol. The server handles the actual interaction with the underlying system.

This separation helps developers define clear boundaries between an agent's reasoning process and the operations it can execute.

How Tool Discovery and Execution Work

One important capability of MCP is tool discovery.

Rather than requiring every available function to be manually integrated into the assistant's application logic, MCP allows clients to discover the tools exposed by a server.

The process generally involves three steps.

1. Tool discovery

The MCP client sends a tools/list request.

The server responds with tool definitions, including their names, descriptions, and input schemas.

2. Tool selection

The AI application uses the available tool definitions to determine which operation is appropriate for the user's request.

For example, if someone asks, "What is the current status of ticket 1042?", the application may select get_ticket_status.

3. Tool execution

The client sends a tools/call request containing the tool name and required arguments.

The MCP server validates the request, retrieves the information from the appropriate backend, and returns the result.

The application can then use that result to prepare its response.

A significant advantage here is that the agent interacts through a defined interface rather than having direct access to the entire backend.

What Makes an MCP Server Read-Only?

At first, the answer seems straightforward: simply avoid creating tools that update or delete data.

But there is an important distinction between a tool being described as read-only and actually enforcing read-only behavior.

MCP supports an annotation called readOnlyHint, which indicates that a tool is not expected to modify its environment.

However, this annotation is descriptive. It does not automatically prevent the underlying implementation from performing write operations.

For a stronger implementation, read-only restrictions should be enforced at multiple levels.

At the tool level: Only expose operations needed for retrieving information.

At the application level: Validate inputs, enforce resource ownership, and reject unauthorized requests.

At the database level: Use credentials restricted to read operations, such as SELECT permissions for the required tables or views.

At the API level: Use appropriately scoped credentials and avoid granting unnecessary write capabilities.

This follows the principle of least privilege: each component should receive only the permissions required to perform its intended function.

Importantly, read-only access does not automatically mean unrestricted access to all data is safe. A server that cannot modify records may still expose confidential information if authorization is poorly implemented.

Testing the MCP Server

A server that starts successfully is not necessarily ready for real-world use.

Testing should cover both functionality and permission boundaries.

For this type of implementation, I would focus on five areas:

Tool discovery: Verify that the MCP client can discover the expected tools and their schemas.

Successful execution: Confirm that valid inputs return the correct documents or ticket information.

Input validation: Check how the server handles missing arguments, invalid identifiers, and unexpected input types.

Access control: Ensure that users cannot retrieve records outside their authorized scope.

Read-only enforcement: Confirm that the exposed operations cannot modify the underlying data, including through unexpected inputs or implementation mistakes.

It is also worth testing backend failures, unavailable records, and database connection errors.

These cases matter because agents need predictable tool responses, particularly when the external system cannot complete a request.

Why This Matters in Production

When developing an AI agent, it is tempting to focus primarily on model capabilities: reasoning, planning, memory, and tool selection.

But the reliability of an agent also depends on the systems surrounding the model.

A model might select the correct tool and provide valid arguments, yet the application can still fail because of insufficient authorization, unexpected responses, or poorly defined permissions.

MCP helps standardize the interaction between applications and tools, but it does not replace backend engineering practices.

Authentication, authorization, validation, logging, error handling, and least-privilege access remain essential responsibilities of the implementation.

For that reason, a read-only MCP server is a useful foundation before introducing actions that create, update, or delete records.

It allows developers to establish reliable tool discovery and execution while keeping the operational scope relatively limited.

Final Thoughts

The most important lesson is that building an AI agent is not simply about giving an LLM more tools.

It is about designing the right interfaces and controlling what those tools are permitted to do.

A well-designed MCP server should expose clear capabilities, validate requests, enforce permissions, and return predictable results.

The goal is not to give AI agents unrestricted access to external systems. It is to give them precisely the capabilities they need to complete a task safely and reliably.

That is a principle worth carrying forward as AI applications move from experimental prototypes to production systems.

📰 Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.