powertools-lambda-python icon indicating copy to clipboard operation
powertools-lambda-python copied to clipboard

Feature request: MCP server event handler

Open ran-isenberg opened this issue 7 months ago • 23 comments

Use case

i'd like to be able to write serverless MCP servers with ease and integrate all my powertools capabilities: swagger, event validation, event resolver, logger, tracing, metrics etc.

Solution/User Experience

I'd like to have UX like in https://docs.powertools.aws.dev/lambda/python/latest/core/event_handler/api_gateway/#response-auto-serialization but combined with the official python SDK for MCP server with all the right config to make it work on a lambda.

# server.py
from aws_lambda_powertools.event_handler import MCPResolver

mcp = MCPResolver('myMcp)

# Add an addition tool
@mcp.tool()
def add(a: int, b: int) -> int:
    """Add two numbers"""
    return a + b


# Add a dynamic greeting resource
@mcp.resource("greeting://{name}")
def get_greeting(name: str) -> str:
    """Get a personalized greeting"""
    return f"Hello, {name}!"


# You can continue to use other utilities just as before
@logger.inject_lambda_context()
@tracer.capture_lambda_handler
def lambda_handler(event: dict, context: LambdaContext) -> dict:
    return mcp.resolve(event, context)

Alternative solutions

use unknown open sources like MCPEngine and build my own wrappers for logging/tracer.. i want the powertools edition :)

Acknowledgment

ran-isenberg avatar Apr 25 '25 08:04 ran-isenberg

Hi @ran-isenberg, thank you for opening this feature request.

Everyone is talking about MCPs these days so I am not surprised to see a feature request in this space finally arriving to our project.

I am wondering if you could share - either here or privately via email - whether there are specific use cases that you're considering at CyberArk that this MCP resolver would help with.

Also on a more technical level, I'm wondering if you had any opinions/suggestions on how we should handle authentication with this type of resolver, the spec for MCP is still very blurry around this type of concerns.

Finally, from a governance standpoint, before implementing this we'll have to discuss how to release this. The Powertools for AWS brand has certain commitments around stability and versioning (highlighted in our wiki) that I'm not sure would fit with a spec still very much in flux like MCP.

dreamorosi avatar Apr 25 '25 16:04 dreamorosi

We can talk over discord if you wish, but yeah I have some ideas and needs :) As more auth/authz. When you place it behind API GW, you can use WAF, IAM auth/cognito/any JWT token authorization. See https://github.com/mikegc-aws/Lambda-MCP-Server - he added a custom authorizer. Also MCPEngine added support for idp token validatioin. I've been trying to build a proper cookbook for this with powertools but lack of official SDK support, and seeing lots of sh*tty implementations (that I cant recommend), and the fact that this is sooo new for serverless (protocol changed this week to support non streaming HTTP), leaves me wanting the Powertools treatment :)

ran-isenberg avatar Apr 25 '25 16:04 ran-isenberg

Hey @ran-isenberg and @dreamorosi have you seen this from Mike Chambers: https://github.com/mikegc-aws/Lambda-MCP-Server

This might be of interest - I think this could be a good thing to import into Powertools, to be honest.

walmsles avatar Apr 30 '25 11:04 walmsles

Hey @ran-isenberg and @dreamorosi have you seen this from Mike Chambers: https://github.com/mikegc-aws/Lambda-MCP-Server

This might be of interest - I think this could be a good thing to import into Powertools, to be honest.

I did, i dont want to offend anyone but it's a nice poc level code. I got it working with powertools. However, i think i'd rather use official mcp python SDK to support http non streaming mode.

Waiting for this: https://github.com/modelcontextprotocol/python-sdk

ran-isenberg avatar Apr 30 '25 11:04 ran-isenberg

Hey @walmsles & @ran-isenberg - would you two be available for a quick call in the next week or two?

I'd love to pick your brain on this topic. If we can manage to do it in one call it'd be amazing, but if time zones are a challenge I'm happy to also do one for each.

dreamorosi avatar May 01 '25 08:05 dreamorosi

two weeks from now should be fine, talk to me on discord next week, it's a holiday now :)

ran-isenberg avatar May 01 '25 09:05 ran-isenberg

Following along to get updates from this thread.

ShahNewazKhan avatar Jun 07 '25 17:06 ShahNewazKhan

Official was released 2 weeks ago: https://github.com/awslabs/mcp/tree/main/src/mcp-lambda-handler

andormarkus avatar Jun 07 '25 21:06 andormarkus

yeah, seems like they removed the web adapter requirement! I'll built a proper blueprint with CDK around this and share very soon

ran-isenberg avatar Jun 08 '25 04:06 ran-isenberg

Thanks a lot @andormarkus, from what I'm seeing the mcp-lambda-handler does not support streaming. Is that correct?

moise-g avatar Jun 11 '25 08:06 moise-g

it doesnt. The MCP protocol now supports non streaming HTTP.

ran-isenberg avatar Jun 11 '25 08:06 ran-isenberg

@dreamorosi @moise-g @andormarkus I built the template! I used the AWS examples combined them with my own aws lambda handler cookbook and made extra editing... It uses powertools too!

Check it out: https://github.com/ran-isenberg/aws-lambda-mcp-cookbook

ran-isenberg avatar Jun 11 '25 10:06 ran-isenberg

@ran-isenberg Fantastic, thanks for sharing!

For one of my use cases, streaming is a hard requirement. My current idea is to go with Fargate (ECS) + Load Balancer. Would that make sense since it looks like Lambda is a no go for streaming?

moise-g avatar Jun 11 '25 10:06 moise-g

@ran-isenberg Fantastic, thanks for sharing!

For one of my use cases, streaming is a hard requirement. My current idea is to go with Fargate (ECS) + Load Balancer. Would that make sense since it looks like Lambda is a no go for streaming?

Yes, sounds about right. Here's my prod-ready ( i think so) CDK based repo for Fargate + ELB: https://github.com/ran-isenberg/aws-chatbot-fargate-python

ran-isenberg avatar Jun 11 '25 11:06 ran-isenberg

There are definitely some rough edges, but Lambda Web Adapter could be an option here - assuming a few seconds of init are tolerable in this type of workload.

Alternatively, if you aren't too tied to Python, you could switch to Node.js, which does support response streaming with Lambda Function URLs.

dreamorosi avatar Jun 11 '25 11:06 dreamorosi

@dreamorosi @leandrodamascena Ideally, i'd like pwoertools to own this module: https://github.com/ran-isenberg/aws-lambda-mcp-cookbook/tree/main/service/mcp_lambda_handler - that's the engine, event handler basically. then the experience is like this:

https://github.com/ran-isenberg/aws-lambda-mcp-cookbook/blob/main/service/handlers/mcp.py

ran-isenberg avatar Jun 12 '25 17:06 ran-isenberg

Let us talk about it internally - in the next 2 weeks we'll update our roadmap for the back half of the year, so it's a good time to discuss this.

dreamorosi avatar Jun 12 '25 21:06 dreamorosi

Let us talk about it internally - in the next 2 weeks we'll update our roadmap for the back half of the year, so it's a good time to discuss this.

sure. I think the code is rather simple TBH. The only issue is keeping up with MCP protocol changes. The code is not using the latest changes (missing auth). Also, the code could really use some refactors, moving to classes, reducing complexity etc, but it works!

ran-isenberg avatar Jun 13 '25 14:06 ran-isenberg

Just a quick feedback: we've had a few meetings about the roadmap for mid/late 2025 and discussed this issue a bit. While we don't have a decision yet, as we have a few other items to deal with, it's on our radar for a decision soon.

leandrodamascena avatar Jul 03 '25 10:07 leandrodamascena

I'll temporarily re-add the triage label so it's picked up on our next planning meeting.

dreamorosi avatar Jul 04 '25 17:07 dreamorosi

Hi, we have decided to postpone our decision and wait for the New York AWS Summit which will happen in the next 10 days.

Thank you for your patience.

dreamorosi avatar Jul 07 '25 14:07 dreamorosi

@dreamorosi is there planned timeline for this feature?

tb102122 avatar Sep 24 '25 05:09 tb102122

Hi @tb102122, no updates at this time.

At the moment we're focused on adding support for new Lambda managed runtimes as well as other items we'll share in the coming weeks.

In the next couple months we'll start creating the roadmap for 2026, so we'll reassess then.

dreamorosi avatar Sep 29 '25 11:09 dreamorosi