Skip to content

Feature Request: Middleware support for McpServer #1238

@ahammednibras8

Description

@ahammednibras8

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementRequest for a new feature that's not currently supported

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions