Skip to content

Handle rate limiting, add runtime config and live tests for Functions toolset#2071

Open
manvkaur wants to merge 5 commits intomainfrom
manvkaur/updateruntimeconfig
Open

Handle rate limiting, add runtime config and live tests for Functions toolset#2071
manvkaur wants to merge 5 commits intomainfrom
manvkaur/updateruntimeconfig

Conversation

@manvkaur
Copy link
Contributor

@manvkaur manvkaur commented Mar 16, 2026

What does this PR do?

Handle GitHub API rate limiting, add runtime configuration, and live tests for Azure Functions toolset.

Key Fixes:

Rate Limiting Handling:

  • Switch to raw GitHub URLs (~5000 req/hr) after fetching GitHub tree to minimize API usage (60 req/hr unauthenticated)
  • Proper detection via X-RateLimit-Remaining: 0 header (distinguishes from 403 permission errors)

Runtime Configuration:

  • Language list now returns runtimeVersions with supported, preview, and default versions from CDN manifest

Template Fetching (Tree API):

  • Switched from GitHub Contents API to Tree API to handle rate limiting
  • Added 5MB size limit (MaxTreeSizeBytes)
  • 12-hour cache duration for manifests and templates

Live Tests:

  • 2 recorded tests for build pipeline (playback mode) - one per test class that fetches from CDN
  • 18 tests marked [LiveTestOnly] - run only in Live mode
  • Tests cover: language listing, template retrieval, runtime versions, error handling
  • Cache-dependent tests cannot be reliably recorded due to non-deterministic test execution order

Review Feedback Addressed:

Change Description
Null checks Added ArgumentNullException.ThrowIfNull() for httpClientFactory and logger
System.Net using Added using System.Net; instead of fully qualified HttpStatusCode
Private methods Made FetchTemplateFilesAsync private (internal use only)
Exception handling Added catch for InvalidOperationException in ManifestService for size limits
Unused code Removed ConvertToRawGitHubUrl method (not used in production)
JSON consolidation Merged FunctionTemplatesManifestJsonContext into FunctionsJsonContext
Redundant attributes Removed [JsonPropertyName] from 11 model files (CamelCase policy handles it)
User-Agent version Created HttpClientHelper to use real assembly version via AssemblyFileVersionAttribute
Rate limit docs Added comment explaining raw.githubusercontent.com rate limiting (~5000/hr vs 60/hr)
Changelog Changed section from "Features Added" to "Other Changes"

Files Changed:

Area Changes
Services FunctionsService, ManifestService, new HttpClientHelper
Models Removed redundant [JsonPropertyName] from 11 files
JSON Context Consolidated into single FunctionsJsonContext
Tests 118 unit tests (including 3 new HttpClientHelper tests), 20 live tests

GitHub issue number?

Related #1781

Pre-merge Checklist

  • Required for All PRs
    • Read contribution guidelines
    • PR title clearly describes the change
    • Commit history is clean with descriptive messages
    • Added comprehensive tests for new/modified functionality
    • Updated servers/Azure.Mcp.Server/CHANGELOG.md
  • For MCP tool changes:
    • One tool per PR: This PR modifies the Functions toolset only
    • Updated documentation
    • Run .\eng\scripts\Update-AzCommandsMetadata.ps1

manvkaur and others added 2 commits March 16, 2026 14:44
- Fetch templates manifest from Azure CDN with GitHub fallback
- Add runtime version configuration to language metadata
- Add FunctionsCacheDurations for 12-hour template caching
- Add rate limit handling for GitHub template downloads
- Remove deprecated GitHubContentEntry model
- Add TemplateManifest model for CDN manifest structure
- Refactor FunctionsService and LanguageMetadataProvider for CDN support
- Update unit tests for new HTTP-based manifest fetching
- Add live tests for language list and template get commands

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@manvkaur
Copy link
Contributor Author

/azp run mcp - pullrequest - live

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR enhances the Azure Functions toolset by sourcing runtime version metadata from the CDN manifest, switching template file retrieval to GitHub’s Tree API (with caching), and adding recorded live tests to validate end-to-end behavior.

Changes:

  • Fetch runtime versions from manifest.json and expose them via functions_language_list (and related metadata flows).
  • Refactor template file fetching to use GitHub Tree API + raw URLs, add caching, and improve error handling for rate limit/network cases.
  • Add a new recorded live test project for Functions with tests covering language listing and template retrieval.

Reviewed changes

Copilot reviewed 16 out of 16 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
tools/Azure.Mcp.Tools.Functions/src/Services/FunctionsService.cs Implements Tree API fetching, caching, and rate limit/network error handling; adds Tree API models and JSON context entries.
tools/Azure.Mcp.Tools.Functions/src/Services/LanguageMetadataProvider.cs Splits static language metadata from runtime versions; builds runtime-version-driven template parameters.
tools/Azure.Mcp.Tools.Functions/src/Services/ILanguageMetadataProvider.cs Extends API to accept optional manifest runtime versions for language info/validation.
tools/Azure.Mcp.Tools.Functions/src/Services/ManifestService.cs Uses centralized cache duration for manifest caching.
tools/Azure.Mcp.Tools.Functions/src/Services/FunctionsCacheDurations.cs Adds 12-hour cache duration constant used across Functions services.
tools/Azure.Mcp.Tools.Functions/src/Models/TemplateManifest.cs Adds runtimeVersions to the manifest model.
tools/Azure.Mcp.Tools.Functions/src/Models/GitHubContentEntry.cs Removes Contents API model (no longer used).
tools/Azure.Mcp.Tools.Functions/tests/Azure.Mcp.Tools.Functions.UnitTests/Services/FunctionsServiceTests.cs Removes tests tied to deleted Contents API URL builder.
tools/Azure.Mcp.Tools.Functions/tests/Azure.Mcp.Tools.Functions.UnitTests/Services/FunctionsServiceHttpTests.cs Updates service construction for caching; adds Tree API error handling tests.
tools/Azure.Mcp.Tools.Functions/tests/Azure.Mcp.Tools.Functions.UnitTests/Language/LanguageListCommandTests.cs Updates deserialization validation to include manifest-provided runtime versions.
tools/Azure.Mcp.Tools.Functions/tests/Azure.Mcp.Tools.Functions.LiveTests/Azure.Mcp.Tools.Functions.LiveTests.csproj Adds new Functions live test project.
tools/Azure.Mcp.Tools.Functions/tests/Azure.Mcp.Tools.Functions.LiveTests/assets.json Adds recorded-test assets configuration.
tools/Azure.Mcp.Tools.Functions/tests/Azure.Mcp.Tools.Functions.LiveTests/BaseFunctionsCommandLiveTests.cs Adds recorded-test base class for Functions live tests.
tools/Azure.Mcp.Tools.Functions/tests/Azure.Mcp.Tools.Functions.LiveTests/Language/LanguageListCommandLiveTests.cs Adds live tests for language list + runtime versions + template parameters.
tools/Azure.Mcp.Tools.Functions/tests/Azure.Mcp.Tools.Functions.LiveTests/Template/TemplateGetCommandLiveTests.cs Adds live tests for listing templates and fetching template files (incl. runtime-version replacement).
servers/Azure.Mcp.Server/changelog-entries/1773697825677.yaml Adds changelog entry for Functions toolset enhancements.

- Add MaxTreeSizeBytes (5MB) size limit for Tree API response to prevent OOM
- Add IsRateLimited() helper to check X-RateLimit-Remaining header for 403
- Handle truncated tree response with warning log
- Move GitHubTreeResponse and GitHubTreeItem to separate model files
- Refactor live tests for better recording coverage (20 recorded vs 1 skipped)
- Add implicit cache verification test using test proxy recordings

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@manvkaur
Copy link
Contributor Author

/azp run mcp - pullrequest - live

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@manvkaur
Copy link
Contributor Author

/azp run mcp - pullrequest - live

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).


[Fact]
[LiveTestOnly]
public async Task ExecuteAsync_LanguageListThenTemplate_UsesSharedCache()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that most of the Live Tests are marked as [LiveTestOnly]. Can we not have recorded tests for these to have better CI coverage?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The [LiveTestOnly] tests rely on an in-memory cached manifest populated by the first test in each class. xUnit runs tests in non-deterministic order, so in Playback mode a cache-dependent test might run first with an empty cache, try to call the CDN, but have no recorded response.

The CDN functionality is well covered:

  • 2 recorded live tests - validate real CDN integration and manifest structure
  • 118 unit tests - mock HTTP responses to test CDN fetching, caching, fallback logic, error handling, and edge cases

The remaining 18 live tests focus on query variations using cached data and are validated during live mode.

- Add null checks for httpClientFactory and logger with _ prefix
- Add using System.Net and use HttpStatusCode.TooManyRequests
- Make FetchTemplateFilesAsync private (only used internally)
- Add catch for InvalidOperationException in ManifestService
- Change changelog section to Other Changes
- Add comment documenting raw.githubusercontent.com rate limiting (~5000/hr)
- Remove unused ConvertToRawGitHubUrl method
- Consolidate FunctionTemplatesManifestJsonContext into FunctionsJsonContext
- Remove redundant [JsonPropertyName] attributes from 11 model files
- Fix tests to use FunctionsJsonContext for serialization
- Create HttpClientHelper for User-Agent with real assembly version
- Add doc comments explaining [LiveTestOnly] pattern in live tests

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@manvkaur
Copy link
Contributor Author

/azp run mcp - pullrequest - live

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Untriaged

Development

Successfully merging this pull request may close these issues.

3 participants