AI Hints
Home›MCP Tutorial›Limiting Damage
MCP · Lesson 94 of 120

Least Privilege

Only what is needed.

⏱ 2 min read▶ Video at 1:24:11🎓 Free
Watch this lesson in the MCP courseWatch on YouTube ↗Full MCP course

A reporting tool only needs to read orders. If it can also delete them, one tricked model could wipe the database. Least privilege means each tool gets only the access it truly needs.

Least Privilege
From the video: Least Privilege
  • Only reads needed
  • But can delete
  • Give the least access

A read-only connection

We make a small database. The tool's connection opens it in read only mode. Then we try a select, and a delete. Run it. The select works. The delete fails, because the connection can't write.

read_only.pypython
import sqlite3
from mcp.server.mcpserver import MCPServer
from tester import call

db = sqlite3.connect("shop.db")
db.executescript("drop table if exists orders; create table orders(id, status);"
                 "insert into orders values (1042, 'shipped');"); db.commit()
read_only = sqlite3.connect("file:shop.db?mode=ro", uri=True, check_same_thread=False)
mcp = MCPServer("reports")

@mcp.tool()
def run_query(sql: str) -> str:
    """Run a SQL query on the orders table."""
    return str(read_only.execute(sql).fetchall())

for sql in ["select * from orders", "delete from orders"]:
    r = call(mcp, "run_query", {"sql": sql})
    print(sql, "->", "ERROR" if r.is_error else r.content[0].text)
Output
select * from orders -> [(1042, 'shipped')]
delete from orders -> ERROR
Also uses tester.py
import asyncio
from mcp import Client

def call(server, tool, args=None, **options):
    async def go():
        async with Client(server, **options) as client:
            return await client.call_tool(tool, args or {})
    return asyncio.run(go())

def tools(server):
    async def go():
        async with Client(server) as client:
            return (await client.list_tools()).tools
    return asyncio.run(go())

Least privilege, everywhere

Use read only database users. Limit file tools to one folder. Give API keys the smallest scopes. And split risky actions into separate servers, only connected when needed.

keyvalue
Databasesread-only users
Filesone folder
API keyssmall scopes
Risky toolsseparate servers

Summary

Give each tool only the access it needs, so mistakes stay small. Next, secrets.

Key takeaways
  • mode=ro
  • One folder
  • Small scopes
Next up: Secrets — keys stay out of code