# Securing a TypeScript MCP server

URL: https://mcpservercybersecurity.com/guides/securing-a-typescript-mcp-server/
Updated: 2026-11-16

Secure a TypeScript MCP server by declaring strict input schemas on every tool, creating the app with createMcpExpressApp so localhost binding and host validation are on, and setting expectedResource in requireBearerAuth so tokens for other servers are refused. Examples follow the v1 SDK documentation read on 16 Nov 2026.

## How do you validate input in tool handlers?

Declare an input schema on every tool and tighten it until it matches what the tool needs. The SDK's registerTool takes an inputSchema built from Zod types, so the handler receives parsed values instead of raw JSON.

OWASP advises strict JSON Schema, with additionalProperties set to false and a pattern on string fields so only declared parameters and valid formats are accepted. Schemas cannot express everything. In the handler, resolve file paths against an allowed root and reject anything outside it, build no shell commands from arguments, and send URL arguments through an allowlist.

Write tests that send a path outside the allowed root and a URL on a private address range, and assert that both are rejected by the handler.

Keep each schema next to its tool so a change to behavior forces a change to the schema in the same review.

Sources: [MCP TypeScript SDK (v1) documentation: Server](https://raw.githubusercontent.com/modelcontextprotocol/typescript-sdk/v1.x/docs/server.md); [OWASP MCP Security Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/MCP_Security_Cheat_Sheet.html).

## How do you bind to localhost?

Create the Express app with createMcpExpressApp, which uses the default host 127.0.0.1 and turns on DNS rebinding protection. The v1 SDK documentation shows the same protection for host localhost, and shows that passing 0.0.0.0 gives no automatic protection.

If you need custom host handling, add the hostHeaderValidation middleware with a list such as localhost, 127.0.0.1 and your own host name. The MCP transport specification requires servers to validate the Origin header and to answer an invalid one with 403, and recommends binding only to localhost for local servers.

Sources: [MCP TypeScript SDK (v1) documentation: Server](https://raw.githubusercontent.com/modelcontextprotocol/typescript-sdk/v1.x/docs/server.md); [MCP specification: Transports (2025-11-25)](https://modelcontextprotocol.io/specification/2025-11-25/basic/transports).

## How do you verify bearer tokens by audience?

Use requireBearerAuth with your own verifier and set expectedResource to your server's URL. With expectedResource set, a token is accepted only if the verifier reports that resource, and a token for another value, or none, gets 401 invalid_token. Without it, the documentation says AuthInfo.resource is not compared with anything.

This is the audience check the MCP authorization specification requires: servers must validate that access tokens were issued for them, and must not accept tokens issued to anything else. Your verifier should also read the token's audience claim and report the entry that matches your origin.

Fail closed. When the verifier cannot validate the token, or cannot read an audience, return 401 rather than letting the request continue to the tool.

Sources: [MCP TypeScript SDK (v1) documentation: Server](https://raw.githubusercontent.com/modelcontextprotocol/typescript-sdk/v1.x/docs/server.md); [MCP specification: Authorization (2025-11-25)](https://modelcontextprotocol.io/specification/2025-11-25/basic/authorization).

## Which transport should a local server use?

Use stdio for a server that only one local client needs. OWASP notes that stdio avoids a listening endpoint but does not restrict the server process's file system, network or credential access, so sandboxing is still needed.

If you need HTTP on the machine, the specification suggests requiring an authorization token or using unix domain sockets or other restricted IPC.

Sources: [OWASP MCP Security Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/MCP_Security_Cheat_Sheet.html); [MCP specification: Security Best Practices (2025-11-25)](https://modelcontextprotocol.io/specification/2025-11-25/basic/security_best_practices).

## How do you handle tool output and SDK versions?

Validate output before it returns to the model, because output often becomes input to another tool. The SDK supports an outputSchema next to structuredContent, which gives you a place to enforce the shape.

Match examples to the SDK version you install. The v1 documentation imports from @modelcontextprotocol/sdk paths, and the repository's main branch shows a split-package layout, so import paths and helper names can differ.

Sources: [MCP TypeScript SDK (v1) documentation: Server](https://raw.githubusercontent.com/modelcontextprotocol/typescript-sdk/v1.x/docs/server.md); [MCP TypeScript SDK repository](https://github.com/modelcontextprotocol/typescript-sdk); [OWASP MCP Security Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/MCP_Security_Cheat_Sheet.html).

## Sources

- [MCP TypeScript SDK (v1) documentation: Server](https://raw.githubusercontent.com/modelcontextprotocol/typescript-sdk/v1.x/docs/server.md) (raw.githubusercontent.com), read 2026-11-16
- [MCP TypeScript SDK repository](https://github.com/modelcontextprotocol/typescript-sdk) (github.com), read 2026-11-16
- [MCP specification: Transports (2025-11-25)](https://modelcontextprotocol.io/specification/2025-11-25/basic/transports) (modelcontextprotocol.io), read 2026-11-16
- [MCP specification: Authorization (2025-11-25)](https://modelcontextprotocol.io/specification/2025-11-25/basic/authorization) (modelcontextprotocol.io), read 2026-11-16
- [OWASP MCP Security Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/MCP_Security_Cheat_Sheet.html) (cheatsheetseries.owasp.org), read 2026-11-16
- [MCP specification: Security Best Practices (2025-11-25)](https://modelcontextprotocol.io/specification/2025-11-25/basic/security_best_practices) (modelcontextprotocol.io), read 2026-11-16

## Related pages

- [Securing a Python MCP Server: Host Checks and Deployment](/guides/securing-a-python-mcp-server/)
- [Securing a Spring Boot MCP Server: Spring AI Settings](/guides/securing-a-spring-boot-mcp-server/)
- [Tool schema (glossary)](/glossary/tool-schema/)
- [Path traversal (glossary)](/glossary/path-traversal/)
- [All guides](/guides/)
- [Building an Approved MCP Server Catalog for Your Company](/guides/building-an-approved-mcp-server-catalog/)
- [The ranking](/ranking/)
- [Glossary](/glossary/)
