Skip to content

Commit 6af00c4

Browse files
dvir-pashutdanilopscottschreckengaust
authored
Handle Lambda function pagination (#1) (#1310)
Co-authored-by: Danilo Poccia <[email protected]> Co-authored-by: Scott Schreckengaust <[email protected]>
1 parent 38560db commit 6af00c4

File tree

4 files changed

+54
-38
lines changed

4 files changed

+54
-38
lines changed

src/lambda-tool-mcp-server/awslabs/lambda_tool_mcp_server/server.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,14 +264,24 @@ def filter_functions_by_tag(functions, tag_key, tag_value):
264264
return tagged_functions
265265

266266

267+
def get_all_lambda_functions():
268+
"""Retrieve all available Lambda functions using pagination."""
269+
paginator = lambda_client.get_paginator('list_functions')
270+
all_functions = []
271+
272+
for page in paginator.paginate():
273+
all_functions.extend(page.get('Functions', []))
274+
275+
return all_functions
276+
277+
267278
def register_lambda_functions():
268279
"""Register Lambda functions as individual tools."""
269280
try:
270281
logger.info('Registering Lambda functions as individual tools...')
271-
functions = lambda_client.list_functions()
272282

273283
# Get all functions
274-
all_functions = functions['Functions']
284+
all_functions = get_all_lambda_functions()
275285
logger.info(f'Total Lambda functions found: {len(all_functions)}')
276286

277287
# First filter by function name if prefix or list is set

src/lambda-tool-mcp-server/tests/conftest.py

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,35 @@ def mock_lambda_client():
1010
"""Create a mock boto3 Lambda client."""
1111
mock_client = MagicMock()
1212

13-
# Mock list_functions response
14-
mock_client.list_functions.return_value = {
15-
'Functions': [
16-
{
17-
'FunctionName': 'test-function-1',
18-
'FunctionArn': 'arn:aws:lambda:us-east-1:123456789012:function:test-function-1',
19-
'Description': 'Test function 1 description',
20-
},
21-
{
22-
'FunctionName': 'test-function-2',
23-
'FunctionArn': 'arn:aws:lambda:us-east-1:123456789012:function:test-function-2',
24-
'Description': 'Test function 2 description',
25-
},
26-
{
27-
'FunctionName': 'prefix-test-function-3',
28-
'FunctionArn': 'arn:aws:lambda:us-east-1:123456789012:function:prefix-test-function-3',
29-
'Description': 'Test function 3 with prefix',
30-
},
31-
{
32-
'FunctionName': 'other-function',
33-
'FunctionArn': 'arn:aws:lambda:us-east-1:123456789012:function:other-function',
34-
'Description': '', # Empty description
35-
},
36-
]
37-
}
13+
# Mock list_functions paginator response
14+
paginator_mock = MagicMock()
15+
paginator_mock.paginate.return_value = [
16+
{
17+
'Functions': [
18+
{
19+
'FunctionName': 'test-function-1',
20+
'FunctionArn': 'arn:aws:lambda:us-east-1:123456789012:function:test-function-1',
21+
'Description': 'Test function 1 description',
22+
},
23+
{
24+
'FunctionName': 'test-function-2',
25+
'FunctionArn': 'arn:aws:lambda:us-east-1:123456789012:function:test-function-2',
26+
'Description': 'Test function 2 description',
27+
},
28+
{
29+
'FunctionName': 'prefix-test-function-3',
30+
'FunctionArn': 'arn:aws:lambda:us-east-1:123456789012:function:prefix-test-function-3',
31+
'Description': 'Test function 3 with prefix',
32+
},
33+
{
34+
'FunctionName': 'other-function',
35+
'FunctionArn': 'arn:aws:lambda:us-east-1:123456789012:function:other-function',
36+
'Description': '', # Empty description
37+
},
38+
]
39+
}
40+
]
41+
mock_client.get_paginator.return_value = paginator_mock
3842

3943
# Mock list_tags response
4044
def mock_list_tags(Resource):

src/lambda-tool-mcp-server/tests/test_integration.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,17 @@ def test_mcp_initialization(self, mock_lambda_client):
3434
def test_tool_registration(self, mock_lambda_client, mock_create_lambda_tool):
3535
"""Test that Lambda functions are registered as tools."""
3636
# Set up the mock
37-
mock_lambda_client.list_functions.return_value = {
38-
'Functions': [
39-
{
40-
'FunctionName': 'test-function',
41-
'FunctionArn': 'arn:aws:lambda:us-east-1:123456789012:function:test-function',
42-
'Description': 'Test function description',
43-
},
44-
]
45-
}
37+
mock_lambda_client.get_paginator.return_value.paginate.return_value = [
38+
{
39+
'Functions': [
40+
{
41+
'FunctionName': 'test-function',
42+
'FunctionArn': 'arn:aws:lambda:us-east-1:123456789012:function:test-function',
43+
'Description': 'Test function description',
44+
},
45+
]
46+
}
47+
]
4648

4749
# Call the function
4850
register_lambda_functions()

src/lambda-tool-mcp-server/tests/test_server.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -416,8 +416,8 @@ def test_register_with_no_filters(self, mock_create_lambda_tool, mock_lambda_cli
416416
@patch('awslabs.lambda_tool_mcp_server.server.lambda_client')
417417
def test_register_error_handling(self, mock_lambda_client):
418418
"""Test error handling in register_lambda_functions."""
419-
# Make list_functions raise an exception
420-
mock_lambda_client.list_functions.side_effect = Exception('Error listing functions')
419+
# Make get_paginator raise an exception
420+
mock_lambda_client.get_paginator.side_effect = Exception('Error listing functions')
421421

422422
# Should not raise an exception
423423
register_lambda_functions()

0 commit comments

Comments
 (0)