modelcontextprotocol / modelcontextprotocol/typescript-sdk

Feature Request: Middleware support for McpServer

Open
#1,238 14 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement needs decision P3
Dominant language
TypeScript
Stars
13.4k
Forks
2.2k
Avg merge
3d 15h
Merged PRs (30d)
4

Description

Is your feature request related to a problem? Please describe.
I've been
working on a secure MCP server (a "Zero Trust" database bridge) and found myself
needing to apply the same logic across multiple tools—such as logging every
request, checking permissions, or validating schemas.

Currently, I have to manually wrap every single tool handler function to add
this logic. This leads to repetitive code and makes it easy to accidentally miss
a tool. For example, if I want to log every tool call, I have to copy-paste the
logging code into every server.tool(...) definition.

Describe the solution you'd like
would love to see a server.use() method
added to McpServer, similar to how Express or Koa handle middleware. This
would allow developers to define logic once and have it automatically run for
every tool or resource request.

Ideally, it could look something like this:

const server = new McpServer({ name: "my-server", version: "1.0.0" });

// Global logging middleware
server.use(async (ctx, next) => {
    console.log(`Calling tool: ${ctx.request.method}`);
    await next();
});

This would make the code much cleaner and ensure that important checks (like
auth) are never skipped.

Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.

Additional context
I am new to contributing to this project, but I would be
very happy to try implementing this feature if the team thinks it's a good idea!
I'm open to any guidance on how best to fit this into the existing architecture.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading McpServer and the existing server.tool handling to understand where tool and resource requests are dispatched. Compare the current handler flow with the proposed server.use() shape and clarify the middleware context, ordering, and next behavior with the maintainers. Done should mean shared logic can run for every relevant request without wrapping each tool manually.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
backend-api-design
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.