
How to Secure Remote MCP Servers: Authorization, Sessions and Agent Tool Security
Secure remote MCP servers: authentication before session tuning
An MCP server reachable over HTTP is a security boundary, not merely a JSON-RPC adapter. Separate who can call a tool, what that tool may access, and how tool output is treated before deciding whether to retain a session identifier.
Reviewed November 12, 2026: This version limits its recommendations to current sourced transport and authorization guidance. It deliberately avoids presenting stateless operation, hard-coded bearer secrets, HTML escaping or a string-prefix path check as sufficient security controls.
1. Does the protocol prohibit sessions?
The MCP Streamable HTTP transport specification allows servers to assign an MCP session identifier. The transport description includes behavior for session identifiers and their management. A deployment can choose an appropriate state-management design, but do not delete valid session handling solely because an article calls stateless operation mandatory.
Client identity is a separate authorization decision; server-authenticated TLS alone does not prove who sent a request. If mutual TLS is used, validate client certificates server-side instead of relying on encryption alone. The MCP authorization specification separately describes authorization behavior for HTTP-based transport. A session identifier is not a substitute for caller identity or a scoped authorization decision, and obtaining a bearer token does not automatically make every tool safe.
2. Choose an explicit authorization policy
| Layer | Security question | Verification |
|---|---|---|
| Transport TLS | Does the client verify the intended server identity, and is traffic encrypted? | Validate the server certificate and HTTPS reverse-proxy configuration; this does not authenticate the calling client |
| Authentication | Who is the caller? | Trusted issuer, expected audience and expiry checks |
| Tool authorization | May this caller invoke this exact tool on this resource? | Server-side per-tool and per-resource policy |
| Filesystem/database | Is data accessible to the service process at all? | Minimal OS privileges and scoped mounts |
| Output handling | Can returned text influence instructions or commands? | Treat tool content as untrusted data |
Do not ship a hard-coded bearer token in a code sample or enable a fallback secret when an expected environment variable is absent. Use a verified supported authentication library for your protocol version and test missing, invalid, expired and wrong-audience credentials. A comparison such as a constant-time string check is not a substitute for signature validation and authority checks.
3. Agentjacking and malicious tool output
An untrusted tool response can contain text that tries to redirect a coding assistant. HTML-escaping characters or wrapping text in XML does not turn the contents into an authorized system-level instruction. The trustworthy boundary is the application’s handling of tool output as lower-trust data, plus explicit approval and permissions before actions with side effects.
Keep secrets out of returned content and logs. For sensitive operations, apply resource-scoped server-side authorization even if a client has passed an initial bearer-token check. Avoid describing any single prompt template or XML wrapper as eliminating injection risk.
4. File-reading tools need a designed boundary
For a file tool, a string prefix test against an approved directory is inadequate: directories can share path prefixes and filesystem targets can change. Path.resolve() provides path normalization, but a later open can still occur after the filesystem changes. A production file tool should use a vetted containment method appropriate to its OS and threat model, not claim a universal traversal proof from one Python expression.
The Python pathlib documentation describes resolving paths and their relationships. Test symbolic links, directory changes, nonexistent files, short files, permission failures and bytes beyond the expected response limit in an isolated file tree. Never read arbitrary agent-provided paths from the host as a general-purpose remote MCP capability.
5. Deploy and validate before accepting real traffic
- Pin the MCP SDK implementation, transport version and authentication middleware actually used.
- Require an authenticated caller and a separate per-resource authorization rule on each operation.
- For Streamable HTTP, validate the
Originheader on incoming connections as required by the 2025-06-18 transport specification. The requirement addresses DNS-rebinding risk; the specification also advises binding a locally run server to127.0.0.1rather than every interface. Origin checks are not a substitute for authentication. Validate network isolation, proxy headers, client redirects and resource URL handling separately. Do not trust model-supplied URLs implicitly. - Test malformed requests, mismatched session IDs, token expiry and deliberate tool-result injection in a disposable environment.
- Restrict service filesystem and network access at the process/container level. Running as non-root alone does not prove every mounted path is safe.
- Confirm a failed auth check has no tool side effect and no secret is logged.
Limitations: This article contains no runnable authentication middleware or production container command because no exact SDK release, identity provider or deployment environment was supplied and tested. It is a threat-model and test plan, not a claim to have verified OAuth or SSRF defenses in a live MCP deployment.
Official primary sources
- MCP Streamable HTTP and optional session identifiers
- MCP authorization model
- Python pathlib path resolution
Reviewed November 12, 2026. Original publication date preserved; version differences must be checked against the target client/server.