Connect an agent application to tools through a shared protocol
Model Context Protocol is an open protocol for connecting AI applications to tools and contextual resources through a consistent client-server interface. It standardizes discovery and invocation; it does not replace the underlying API or service and does not make a capability automatically trusted or safe.
What You Will Learn
- Trace an MCP request from host and client to server and underlying service.
- Distinguish protocol compatibility from trust and authorization.
- Compare MCP with direct APIs and ordinary function-tool integration.
- Treat exact SDK details as version-dependent implementation choices.
The protocol is not the underlying service
An MCP server exposes supported protocol capabilities—such as tools, resources, or prompts—to a compatible client. The server may then call a separate database, filesystem, search index, or business API that performs the real operation. MCP standardizes the connection and discovery layer; it does not replace that service.
Compatibility does not grant trust. Hosts and servers still need authentication, authorization, user consent, least-privilege scopes, input validation, credential protection, audit logs, and version management. SDK names may evolve, so the stable lesson is the host–client–server–service boundary.
Instructional figure
MCP architecture and its trust boundary
How does an AI host reach an underlying service through MCP?
Authorization / consent / validation boundary
MCP, direct APIs, and ordinary function tools
Discover a capability, request it and handle the response
An MCP server exposes tools, resources, or prompts supported by that implementation.
An MCP client connects the server to an AI host and discovers the advertised capabilities.
The host presents relevant capabilities to the model, which may propose an invocation.
The application validates permission, credentials, arguments, and trust boundaries before the server reaches the underlying API or data source.
Structured results return through the client, while operators manage server versions, compatibility, credentials, and governance separately.
Worked Example: Discovery enables invocation; it does not grant access
An AI host has MCP client functionality connected to a billing MCP server. The server exposes a read_invoice capability backed by a separate billing API. The example uses conceptual messages because exact SDK and wire fields can evolve.
- Discovery: the client learns that read_invoice accepts an invoice identifier. This establishes protocol compatibility, not permission to read every invoice.
- Normal path: the host requests INV-42; the server authenticates the caller and confirms invoices:read scope for account A-7 before asking the underlying billing API. The API returns $320, which the server packages as a structured protocol result.
- Rejection path: a different credential sends the same well-formed request but lacks invoices:read for A-7. The server returns a structured insufficient_scope error and never sends a request to the billing API.
What the result means: The protocol request/result belongs to the MCP boundary; the billing request/result belongs to the underlying service. Least privilege is enforced at the server/service boundary even when discovery and message structure are valid.
One discovered capability, two authorization outcomes
Instructional figure
MCP discovery and invocation are distinct protocol steps
Which messages are protocol interactions, and which call actually reaches the underlying service?
A Realistic Example: Connecting a documentation server
One MCP server exposes approved documentation search. Different compatible AI hosts can use the same server without implementing a unique integration for each host.
Read the Mechanism in Code
Use the label and note to understand what this block represents before reading each line.
# Conceptual MCP tool definition
@server.tool()
def search_docs(query: str, limit: int = 5) -> list[Document]:
"""Search only the approved documentation index."""
authorize_current_user()
return docs.search(query, limit=min(limit, 10))Hands-On Runnable Python Lab: Inspect Discovery, Invocation and a Scope Rejection
An in-memory teaching trace uses MCP-style JSON-RPC envelopes, not a complete MCP server or SDK. Discovery and invocation follow a simulated initialization. Version 2025-11-25 is the explicitly selected example version.
- Print initialize followed by notifications/initialized, distinguishing request from notification.
- Return read_invoice and its input schema.
- Invoke without read scope and confirm the service is not called.
- Invoke with invoices:read and matching account, returning $320. Count one service call.
Discovery, authorization and the underlying service are different steps
Horizontal arrows are messages or service calls; dashed vertical lines are participant lifelines. Denial stops at the server boundary. Initialization happens before the depicted tool operations.
import json
tool = {
"name": "read_invoice",
"description": "Read one invoice in the authenticated account",
"inputSchema": {"type": "object", "properties": {"invoice_id": {"type": "string"}},
"required": ["invoice_id"], "additionalProperties": False},
}
invoices = {"INV-42": {"account": "A-7", "amount_usd": 320}}
service_calls = 0
def invoke(request, scopes, account):
global service_calls
args = request["params"]["arguments"]
if request["params"]["name"] != "read_invoice" or set(args) != {"invoice_id"}:
message, failed = "Invalid tool request", True
elif "invoices:read" not in scopes:
message, failed = "insufficient_scope", True
elif args["invoice_id"] not in invoices or invoices[args["invoice_id"]]["account"] != account:
message, failed = "Invoice unavailable", True
else:
service_calls += 1
message, failed = json.dumps(invoices[args["invoice_id"]]), False
return {"jsonrpc": "2.0", "id": request["id"],
"result": {"content": [{"type": "text", "text": message}], "isError": failed}}
print("client -> server: initialize (version 2025-11-25)")
print("server -> client: version and tools capability accepted (fixture)")
print("client -> server: notifications/initialized")
print("tools/list result:", json.dumps({"tools": [tool]}, sort_keys=True))
request = {"jsonrpc": "2.0", "id": 2, "method": "tools/call",
"params": {"name": "read_invoice", "arguments": {"invoice_id": "INV-42"}}}
for scopes in [set(), {"invoices:read"}]:
response = invoke(request, scopes, "A-7")
print(json.dumps(response, sort_keys=True))
print("underlying service calls:", service_calls)
client -> server: initialize (version 2025-11-25)
server -> client: version and tools capability accepted (fixture)
client -> server: notifications/initialized
tools/list result: {"tools": [{"description": "Read one invoice in the authenticated account", "inputSchema": {"additionalProperties": false, "properties": {"invoice_id": {"type": "string"}}, "required": ["invoice_id"], "type": "object"}, "name": "read_invoice"}]}
{"id": 2, "jsonrpc": "2.0", "result": {"content": [{"text": "insufficient_scope", "type": "text"}], "isError": true}}
{"id": 2, "jsonrpc": "2.0", "result": {"content": [{"text": "{\"account\": \"A-7\", \"amount_usd\": 320}", "type": "text"}], "isError": false}}
underlying service calls: 1What This Run Shows
- The arguments are identical; trusted authorization context changes the outcome.
- Denial uses isError=True in a tool result. A malformed protocol envelope is a different error class.
- The counter proves discovery and rejection did not themselves read invoice data.
MCP improves interoperability at a connection boundary. It does not make a tool safe, a source authoritative or the underlying API unnecessary.
From the Exercise to an Application
Experiment: Keep invoices:read but change the account to A-99.
The tool returns Invoice unavailable without calling the service. Scope and record ownership are separate gates.
Summary / Key Takeaways
- MCP standardizes how compatible AI hosts and clients discover and invoke capabilities exposed by MCP servers.
- An MCP server can expose protocol capabilities such as tools, resources, and prompts, while the underlying API or data source remains a separate system.
- MCP improves interoperability by reducing one-off integration work between hosts and compatible servers.
- Protocol compatibility is not the same as trust: authentication, authorization, consent, validation, and credentials still matter.
- Clients should request only the scopes and capabilities needed for the task, following least-privilege principles.
- Versioning and compatibility must be managed because protocol and SDK details evolve.
- MCP complements tool calling; it does not replace the application’s responsibility for safe execution and governance.
Verified Further Reading
These primary research papers and current official specifications support the concepts and implementation guidance in this lesson. The explanations, analogies, exercises, and code above remain original teaching material.
Model Context Protocol: Specification (2026-07-28)
The authoritative, versioned protocol specification for message patterns, capabilities, authorization, and compatibility.
Model Context Protocol: Server Tools (2026-07-28)
Normative tool discovery and invocation guidance, including validation, access control, human approval, and untrusted tool metadata.