Repository navigation
Add Azure IoT Hub device and query read tools - #3180
Merged
Merged
Conversation
Adds six read-only IoT Hub commands: device show, device stats, device twin get, query run, query compile, and query discover. Includes options, IIoTHubDeviceService/IoTHubDeviceService, query compiler and field discoverer, models, JSON context, setup registration, GetIoTHubKeys on IIoTHubService, package references, command docs, e2e prompts, consolidated tool mapping, changelog entry, and unit tests.
|
Azure Pipelines: Successfully started running 1 pipeline(s). There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds a new set of read-only Azure IoT Hub “device registry + query” tools to the Azure MCP server, including data-plane calls for device identity/twin/statistics and a local query compile/discover workflow to help agents construct valid IoT Hub queries.
Changes:
- Added IoT Hub device registry commands (show, stats, twin get) and IoT Hub query commands (run, compile, discover), plus DI registration and JSON source-gen updates.
- Introduced a new
IIoTHubDeviceService/IoTHubDeviceServicethat resolves hub hostname + SAS keys via ARM, then calls IoT Hub REST endpoints for device/twin/query/statistics. - Updated documentation, consolidated tool mapping, changelog entry, and added unit tests for the new commands.
Reviewed changes
Copilot reviewed 47 out of 47 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| tools/Azure.Mcp.Tools.IoTHub/tests/Azure.Mcp.Tools.IoTHub.UnitTests/Query/IoTHubQueryRunCommandTests.cs | Adds unit tests for the query run command behavior and validation. |
| tools/Azure.Mcp.Tools.IoTHub/tests/Azure.Mcp.Tools.IoTHub.UnitTests/Query/IoTHubQueryDiscoverCommandTests.cs | Adds unit tests for query field discovery command behavior and error handling. |
| tools/Azure.Mcp.Tools.IoTHub/tests/Azure.Mcp.Tools.IoTHub.UnitTests/Query/IoTHubQueryCompileCommandTests.cs | Adds unit tests for compiling structured filters into IoT Hub query strings. |
| tools/Azure.Mcp.Tools.IoTHub/tests/Azure.Mcp.Tools.IoTHub.UnitTests/Device/IoTHubDeviceTwinGetCommandTests.cs | Adds unit tests for retrieving device twins from IoT Hub. |
| tools/Azure.Mcp.Tools.IoTHub/tests/Azure.Mcp.Tools.IoTHub.UnitTests/Device/IoTHubDeviceStatisticsCommandTests.cs | Adds unit tests for device registry statistics retrieval. |
| tools/Azure.Mcp.Tools.IoTHub/tests/Azure.Mcp.Tools.IoTHub.UnitTests/Device/IoTHubDeviceShowCommandTests.cs | Adds unit tests for device identity retrieval. |
| tools/Azure.Mcp.Tools.IoTHub/src/Services/IoTHubService.cs | Extends ARM-based IoT Hub service to also list IoT Hub SAS keys. |
| tools/Azure.Mcp.Tools.IoTHub/src/Services/IoTHubDeviceService.cs | Implements data-plane IoT Hub REST calls (device/twin/query/stats) with caching + timeouts. |
| tools/Azure.Mcp.Tools.IoTHub/src/Services/IIoTHubService.cs | Adds a service contract for retrieving IoT Hub SAS keys. |
| tools/Azure.Mcp.Tools.IoTHub/src/Services/IIoTHubDeviceService.cs | Introduces new service contract for device registry and query operations. |
| tools/Azure.Mcp.Tools.IoTHub/src/Query/IoTHubQueryFieldDiscoverer.cs | Adds logic to discover queryable fields/types/examples from sampled twin documents. |
| tools/Azure.Mcp.Tools.IoTHub/src/Query/IoTHubQueryCompiler.cs | Adds a structured predicate compiler to generate valid IoT Hub query strings. |
| tools/Azure.Mcp.Tools.IoTHub/src/Options/Query/IoTHubQueryRunOptions.cs | Adds options for running queries (query, max count, continuation token, hub, RG, etc.). |
| tools/Azure.Mcp.Tools.IoTHub/src/Options/Query/IoTHubQueryDiscoverOptions.cs | Adds options for field discovery sampling (hub, RG, max count, etc.). |
| tools/Azure.Mcp.Tools.IoTHub/src/Options/Query/IoTHubQueryCompileOptions.cs | Adds options for local query compilation from JSON filters (+ optional discovered fields). |
| tools/Azure.Mcp.Tools.IoTHub/src/Options/Device/IoTHubDeviceTwinGetOptions.cs | Adds options for device twin get (device id, hub, RG, subscription/tenant). |
| tools/Azure.Mcp.Tools.IoTHub/src/Options/Device/IoTHubDeviceStatisticsOptions.cs | Adds options for device statistics (hub, RG, subscription/tenant). |
| tools/Azure.Mcp.Tools.IoTHub/src/Options/Device/IoTHubDeviceShowOptions.cs | Adds options for device show (device id, hub, RG, subscription/tenant). |
| tools/Azure.Mcp.Tools.IoTHub/src/Models/RegistryStatistics.cs | Adds model for IoT Hub registry statistics response. |
| tools/Azure.Mcp.Tools.IoTHub/src/Models/QueryPredicate.cs | Adds structured predicate model for query compilation. |
| tools/Azure.Mcp.Tools.IoTHub/src/Models/QueryDiscoveredFields.cs | Adds model for discovered fields grouped by scope. |
| tools/Azure.Mcp.Tools.IoTHub/src/Models/QueryDiscoveredField.cs | Adds model for a single discovered field (path/type/examples). |
| tools/Azure.Mcp.Tools.IoTHub/src/Models/QueryCompileRequest.cs | Adds model representing a full compile request (filters/from/top/logical op/discovered fields). |
| tools/Azure.Mcp.Tools.IoTHub/src/Models/PredicateScope.cs | Adds enum for predicate scope (device/tags/desired/reported). |
| tools/Azure.Mcp.Tools.IoTHub/src/Models/PredicateOperator.cs | Adds enum for predicate operators. |
| tools/Azure.Mcp.Tools.IoTHub/src/Models/IoTHubQueryRunResult.cs | Adds result model for query run (items/count/hasMore/continuationToken/message). |
| tools/Azure.Mcp.Tools.IoTHub/src/Models/IoTHubQueryRequest.cs | Adds request model for IoT Hub query endpoint. |
| tools/Azure.Mcp.Tools.IoTHub/src/Models/IoTHubQueryPage.cs | Adds internal page model for query results + continuation. |
| tools/Azure.Mcp.Tools.IoTHub/src/Models/IoTHubQueryDiscoverResult.cs | Adds result model for discovered fields + sampling metadata. |
| tools/Azure.Mcp.Tools.IoTHub/src/Models/IoTHubQueryCompileResult.cs | Adds result model for compiled query + maxCount hint. |
| tools/Azure.Mcp.Tools.IoTHub/src/Models/IoTHubKey.cs | Adds internal model for IoT Hub SAS keys retrieved from ARM. |
| tools/Azure.Mcp.Tools.IoTHub/src/Models/IoTHubDeviceCountStats.cs | Adds model for device-count metric stats (snapshot/peak/average). |
| tools/Azure.Mcp.Tools.IoTHub/src/Models/DeviceTwin.cs | Adds models for device identity/twin (and related nested types). |
| tools/Azure.Mcp.Tools.IoTHub/src/IoTHubSetup.cs | Registers new services and command groups/subcommands for iothub device/twin/query. |
| tools/Azure.Mcp.Tools.IoTHub/src/Commands/Query/IoTHubQueryRunCommand.cs | Implements query run command (single-page behavior, max-count cap, continuation token validation). |
| tools/Azure.Mcp.Tools.IoTHub/src/Commands/Query/IoTHubQueryDiscoverCommand.cs | Implements query discover command (sample + field discovery). |
| tools/Azure.Mcp.Tools.IoTHub/src/Commands/Query/IoTHubQueryCompileCommand.cs | Implements local query compile command (JSON parsing + validation + compilation). |
| tools/Azure.Mcp.Tools.IoTHub/src/Commands/IoTHubJsonContext.cs | Expands JSON source-generation registrations for new models/results. |
| tools/Azure.Mcp.Tools.IoTHub/src/Commands/Device/IoTHubDeviceTwinGetCommand.cs | Implements device twin get command. |
| tools/Azure.Mcp.Tools.IoTHub/src/Commands/Device/IoTHubDeviceStatisticsCommand.cs | Implements device statistics command. |
| tools/Azure.Mcp.Tools.IoTHub/src/Commands/Device/IoTHubDeviceShowCommand.cs | Implements device show command and its metadata/description. |
| tools/Azure.Mcp.Tools.IoTHub/src/Azure.Mcp.Tools.IoTHub.csproj | Adds the Azure.ResourceManager.IotHub package reference. |
| servers/Azure.Mcp.Server/src/Resources/consolidated-tools.json | Adds a consolidated “get_azure_iot_hub_devices” tool mapping to the new commands. |
| servers/Azure.Mcp.Server/docs/e2eTestPrompts.md | Adds e2e prompts for the new IoT Hub device and query commands. |
| servers/Azure.Mcp.Server/docs/azmcp-commands.md | Adds CLI documentation entries for the new IoT Hub commands. |
| servers/Azure.Mcp.Server/changelog-entries/Iris20050110-iothub-read-tools.yaml | Adds changelog entry describing the new IoT Hub read tools. |
| Directory.Packages.props | Adds central package version for Azure.ResourceManager.IotHub. |
Comments suppressed due to low confidence (2)
tools/Azure.Mcp.Tools.IoTHub/src/Services/IoTHubDeviceService.cs:174
- HttpResponseMessage should be disposed to promptly release the underlying connection/resources. This method currently keeps the response undisposed, unlike RunQuery which disposes its response.
tools/Azure.Mcp.Tools.IoTHub/src/Services/IoTHubDeviceService.cs:297 - HttpResponseMessage should be disposed to promptly release the underlying connection/resources. This method currently keeps the response undisposed after reading, which can increase socket pressure under repeated use.
Update device show description Co-authored-by: Copilot Autofix powered by AI <[email protected]>
- Add shared IoTHubQueryLimits.MaxPageSize and reference it from query run and compile so the compile page-size hint cannot exceed the run cap - Split DeviceTwin.cs into one type per file (DeviceIdentity, DeviceAuthentication, DeviceCapabilities, TwinProperties, TwinPatch, TwinPatchProperties) - Dispose HttpResponseMessage in IoTHubDeviceService GET calls (GetDevice, GetDeviceTwin, GetDeviceStatistics) - Route discover/compile field-path validation through shared IoTHubQueryCompiler.IsValidFieldPath so discover never emits a path compile would reject; add unit tests
- Fold 'iothub query discover' and 'iothub query compile' into 'iothub query run' (accepts --query or --filters; a bare SELECT * also returns a discoveredFields catalog). - Switch device/twin/query/stats data-plane auth to Microsoft Entra ID bearer tokens; remove SAS listKeys, 15-min connection caching, and the IoTHubKey model. - Route data-plane calls through an Azure.Core retry pipeline so RetryPolicyOptions governs transient retries. - Add actionable HTTP error guidance (ExplainStatusCode), e.g. 403 -> assign IoT Hub Data Reader. - Strengthen IoT Hub unit tests and add pure IoTHubQueryCompiler tests. - Update azmcp-commands.md, e2eTestPrompts.md, consolidated-tools.json, and changelog.
# Conflicts: # servers/Azure.Mcp.Server/docs/e2eTestPrompts.md
# Conflicts: # servers/Azure.Mcp.Server/README.md # servers/Azure.Mcp.Server/docs/e2eTestPrompts.md # servers/Azure.Mcp.Server/src/Resources/consolidated-tools.json # tools/Azure.Mcp.Tools.IoTHub/src/Azure.Mcp.Tools.IoTHub.csproj # tools/Azure.Mcp.Tools.IoTHub/src/Commands/IoTHubJsonContext.cs # tools/Azure.Mcp.Tools.IoTHub/src/IoTHubSetup.cs # tools/Azure.Mcp.Tools.IoTHub/src/Services/IIoTHubDeviceService.cs # tools/Azure.Mcp.Tools.IoTHub/src/Services/IoTHubDeviceService.cs
A prior merge left azmcp-commands.md with a stale version: ~51 commands were missing (storage, storagesync, virtualdesktop, workbooks, azureterraform, bicepschema, cloudarchitect, advisor, wellarchitectedframework, subscription) and 3 removed commands lingered. Rebuilt the doc from main plus the IoTHub readtools commands (device show, device stats, device twin get, query run) and regenerated tool metadata so query run is correctly non-idempotent. Add brien, detag, notjson to IoTHub cspell.yaml for SpellCheck.
…d failure Co-authored-by: Iris20050110 <[email protected]>
Co-authored-by: Iris20050110 <[email protected]>
Nothing references this package (no PackageReference or namespace usage anywhere); the IoTHub tools use base Azure.ResourceManager plus Resource Graph/REST. Removing it drops the Directory.Packages.props change so CI narrows the test/eval scope to the IoTHub toolset instead of building everything.
'Get the device registry for IoT Hub <hub> in subscription <sub>' structurally matches the hub_get prompts and intermittently routed to iothub_hub_get. Reword to 'List the device registry ...' so the list intent is unambiguous while still exercising the 'device registry' phrasing. The two other device_list prompts already cover the natural phrasings.
adarsh1
reviewed
Aug 21, 2026
adarsh1
reviewed
Aug 21, 2026
adarsh1
reviewed
Aug 21, 2026
adarsh1
reviewed
Aug 21, 2026
adarsh1
reviewed
Aug 21, 2026
adarsh1
reviewed
Aug 21, 2026
adarsh1
reviewed
Aug 21, 2026
adarsh1
reviewed
Aug 21, 2026
adarsh1
reviewed
Aug 21, 2026
adarsh1
reviewed
Aug 21, 2026
adarsh1
reviewed
Aug 21, 2026
- iothub query run pages through the registry internally instead of returning a continuation token to the caller; --max-count now caps the total result set and the tool returns an error when the cap truncates results - Add BaseIoTHubCommand to centralize timeout status mapping and rewire the device commands to it - Mark device stats and twin get as non-idempotent and sync consolidated-tools.json - Update azmcp-commands.md and e2eTestPrompts.md
Restore main's prompt wording for all non-IoT Hub tools (the branch had reverted them to 'Use <tool> to ...' phrasing) while keeping the branch's IoT Hub read-tool prompts.
Replace the 'Discover the queryable device twin fields' prompt (which routed to iothub_device_twin_get and failed the Vally tool-calls grader) with a 'find devices where tag ...' filter prompt that matches the query_run description.
Drop 'device registry' and 'all devices' wording from two device_list prompts so they don't collide with query_run in the Vally tool-selection eval.
- Increase query-run timeout from 30s to 100s - Reject unsupported --filters scopes with an actionable message pointing to raw --query SQL - Cap raw --query length (10k chars) and --filters predicate count (100) as defense-in-depth - Remove unused TwinPatch/TwinPatchProperties models - Add unit tests covering the new validations
jichang1
reviewed
Aug 25, 2026
KarishmaGhiya
approved these changes
Aug 27, 2026
Resolve IoTHubDeviceService conflicts and align the IoT Hub read tools with main's removal of per-call retry-policy configuration: - ListDevices follows main (no retryPolicy; hostname+token resolved via ResolveHubAccessAsync helper, default retry pipeline). - Removed retryPolicy from GetDevice/GetDeviceTwin/RunQuery/GetDeviceStatistics, IIoTHubDeviceService, the device/query options and commands, and IoTHubQueryDiscovery, since main removed the BaseAzureService.ConfigureRetryPolicy wrapper and the retry options from the IoT Hub device path. - Updated unit tests to drop the RetryPolicyOptions argument matchers. IoTHub project builds clean; 87 unit tests pass.
KarishmaGhiya
approved these changes
Aug 31, 2026
18 of 21 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds six read-only IoT Hub commands: device show, device stats, device twin get, query run, query compile, and query discover. Includes options, IIoTHubDeviceService/IoTHubDeviceService, query compiler and field discoverer, models, JSON context, setup registration, GetIoTHubKeys on IIoTHubService, package references, command docs, e2e prompts, consolidated tool mapping, changelog entry, and unit tests.
What does this PR do?
[Provide a clear, concise description of the changes]Azure IoT Hub to Azure MCP Server
This is a read-only tool set which includes commands device show, twin get, device stats, and query run that reads devices registry and twin APIs in Iot Hub. The documentation will be published at https://learn.microsoft.com/en-us/cli/azure/iot/hub?view=azure-cli-latest#az-iot-hub-show
[Add additional context, screenshots, or information that helps reviewers]GitHub issue number?
[Link to the GitHub issue this PR addresses]#2917
Pre-merge Checklist
servers/Azure.Mcp.Server/README.mdand/orservers/Fabric.Mcp.Server/README.mddocumentationREADME.mdchanges running the script./eng/scripts/Process-PackageReadMe.ps1. See Package READMEToolDescriptionEvaluatorand obtained a score of0.4or more and a top 3 ranking for all related test promptsconsolidated-tools.jsonbreaking-changelabelservers/Azure.Mcp.Server/docs/azmcp-commands.md./eng/scripts/Update-AzCommandsMetadata.ps1to update tool metadata inazmcp-commands.md(required for CI)servers/Azure.Mcp.Server/docs/e2eTestPrompts.mdcrypto mining, spam, data exfiltration, etc.)/azp run mcp - pullrequest - liveto run Live Test Pipeline