### Install Dependencies and Start Sample Source: https://github.com/microsoftdocs/windows-ai-docs/blob/docs/docs/mcp/quickstart-mcp-host.md Install project dependencies using npm and start the sample application. ```bash npm install npm run start ``` -------------------------------- ### Download and Install Execution Provider with Progress (C/C++) Source: https://github.com/microsoftdocs/windows-ai-docs/blob/docs/docs/new-windows-ml/initialize-execution-providers.md Use `WinMLEpEnsureReadyAsync` to start the download, providing completion and progress callback functions. `WinMLAsyncGetStatus` can be used to wait for completion. Check the HRESULT returned by `WinMLAsyncGetStatus`. ```c #include #include #include #include // Progress callback — called periodically during download void CALLBACK OnProgress(WinMLAsyncBlock* async, double progress) { // progress is out of 100, convert to 0-1 range double normalizedProgress = progress / 100.0; // Display the progress to the user std::cout << std::format("Progress: {:.0f}\\n", normalizedProgress * 100); } // Completion callback — called when the download finishes void CALLBACK OnComplete(WinMLAsyncBlock* async) { HRESULT hr = WinMLAsyncGetStatus(async, FALSE); if (SUCCEEDED(hr)) { std::cout << "Download complete!\\n"; } else { std::cout << std::format("Download failed: 0x{:08X}\\n", static_cast(hr)); } } // Start the async download with progress WinMLAsyncBlock async = {}; async.callback = OnComplete; async.progress = OnProgress; HRESULT hr = WinMLEpEnsureReadyAsync(ep, &async); if (SUCCEEDED(hr)) { // Wait for the async operation to complete WinMLAsyncGetStatus(&async, TRUE); } WinMLAsyncClose(&async); ``` -------------------------------- ### Download and Install Execution Provider with Progress (C++/WinRT) Source: https://github.com/microsoftdocs/windows-ai-docs/blob/docs/docs/new-windows-ml/initialize-execution-providers.md Start an asynchronous EP download with `EnsureReadyAsync()`. Attach a progress callback for UI updates and use `.get()` to wait for completion. Check the `Status` property of the result. ```cppwinrt // Start the download and install of a NotPresent EP auto operation = provider.EnsureReadyAsync(); // Listen to progress callback operation.Progress([this](auto const& asyncInfo, double progressInfo) { // Dispatch to UI thread (varies based on UI platform) dispatcherQueue.TryEnqueue([this, progressInfo]() { // progressInfo is out of 100, convert to 0-1 range double normalizedProgress = progressInfo / 100.0; // Display the progress to the user Progress(normalizedProgress); }); }); // Await for the download and install to complete auto result = operation.get(); // Check that the download and install was successful bool installed = result.Status() == ExecutionProviderReadyResultState::Success; ``` -------------------------------- ### Download and Install Execution Provider (Python) Source: https://github.com/microsoftdocs/windows-ai-docs/blob/docs/docs/new-windows-ml/initialize-execution-providers.md Use `ensure_ready_async()` to download and install a specific execution provider. Check the `status` property of the returned result to verify success. ```python # Download and install a NotPresent EP result = provider.ensure_ready_async().get() # Check that the download and install was successful installed = result.status == winml.ExecutionProviderReadyResultState.SUCCESS ``` -------------------------------- ### Get all EPs and log their names and versions in C# Source: https://github.com/microsoftdocs/windows-ai-docs/blob/docs/docs/new-windows-ml/versioning.md Use this C# code to iterate through all compatible execution providers on a device and log their names and installed versions. If an EP is not installed, it will indicate 'Not installed'. ```csharp // Get all EPs compatible with this device var providers = ExecutionProviderCatalog.GetDefault().FindAllProviders(); // For each provider foreach (var provider in providers) { // Log the name Debug.WriteLine($"Windows ML EP: {provider.Name}"); // Log the version if (provider.PackageId != null) { var v = provider.PackageId.Version; Debug.WriteLine($"Version: {v.Major}.{v.Minor}.{v.Build}.{v.Revision}"); } else { Debug.WriteLine("Version: Not installed"); } } ``` -------------------------------- ### Install Foundry Local WinML and OpenAI NuGet Packages Source: https://github.com/microsoftdocs/windows-ai-docs/blob/docs/docs/foundry-local/get-started.md Install the Microsoft.AI.Foundry.Local.WinML NuGet package for local LLM execution on Windows and the Betalgo.Ranul.OpenAI package for chat-related types. ```bash dotnet add package Microsoft.AI.Foundry.Local.WinML --version 1.0.0 dotnet add package Betalgo.Ranul.OpenAI --version 9.1.0 ``` -------------------------------- ### Install Foundry Local CLI Source: https://github.com/microsoftdocs/windows-ai-docs/blob/docs/docs/foundry-local/get-started.md Install the Foundry Local command-line interface using winget. After installation, close and reopen your terminal to ensure the 'foundry' command is available in your PATH. ```powershell winget install Microsoft.FoundryLocal ``` -------------------------------- ### Install and Register All Certified EPs (C#) Source: https://github.com/microsoftdocs/windows-ai-docs/blob/docs/docs/new-windows-ml/initialize-execution-providers.md Use this method to download and install all EPs compatible with the user's device and register them with ONNX Runtime in a single call. This is the simplest option for most applications. ```csharp using Microsoft.Windows.AI.MachineLearning; // Get the default ExecutionProviderCatalog var catalog = ExecutionProviderCatalog.GetDefault(); // Ensure execution providers compatible with device are present (downloads if necessary) // and then registers all present execution providers with ONNX Runtime await catalog.EnsureAndRegisterCertifiedAsync(); ``` -------------------------------- ### Download and Install Execution Provider (C#) Source: https://github.com/microsoftdocs/windows-ai-docs/blob/docs/docs/new-windows-ml/initialize-execution-providers.md Downloads and installs a specific execution provider if its ReadyState is 'NotPresent'. This is used when you need to ensure a particular EP is available for use. ```csharp // Download and install a NotPresent EP var result = await provider.EnsureReadyAsync(); // Check that the download and install was successful bool installed = result.Status == ExecutionProviderReadyResultState.Success; ``` -------------------------------- ### Install Foundry Local SDK Source: https://github.com/microsoftdocs/windows-ai-docs/blob/docs/docs/foundry-local/get-started.md Install the appropriate Foundry Local SDK package for your operating system. Choose either the Windows-specific package with hardware acceleration or the cross-platform package. ```bash pip install foundry-local-sdk-winml # Windows — includes hardware acceleration (recommended on Windows) pip install foundry-local-sdk # macOS/Linux, or Windows without hardware acceleration ``` -------------------------------- ### Install Windows SDK with WinGet Source: https://github.com/microsoftdocs/windows-ai-docs/blob/docs/docs/mcp/servers/mcp-windows-identity.md Install the latest Windows SDK using WinGet, which includes SignTool.exe. ```powershell winget install Microsoft.WindowsSDK.10.0.26100 ``` -------------------------------- ### Download and Install Execution Provider (C++/WinRT) Source: https://github.com/microsoftdocs/windows-ai-docs/blob/docs/docs/new-windows-ml/initialize-execution-providers.md Downloads and installs a specific execution provider if its ReadyState is 'NotPresent'. This is used when you need to ensure a particular EP is available for use. ```cppwinrt // Download and install a NotPresent EP auto result = provider.EnsureReadyAsync().get(); // Check that the download and install was successful bool installed = result.Status() == ExecutionProviderReadyResultState::Success; ``` -------------------------------- ### Install MCP Bundle Tooling Package Source: https://github.com/microsoftdocs/windows-ai-docs/blob/docs/docs/mcp/servers/mcp-mcpb.md Installs the MCPB CLI NPM package globally. Ensure you have the .NET SDK installed. ```powershell dotnet tool install -g mcpb.cli ``` -------------------------------- ### Automated Dependency Installation with Winget Source: https://github.com/microsoftdocs/windows-ai-docs/blob/docs/docs/apis/get-started.md Run this command in Windows Terminal to automatically install necessary dependencies for Windows AI development. This includes enabling Developer Mode, installing Visual Studio with required workloads, and the Windows App SDK. ```cmd winget configure https://raw.githubusercontent.com/microsoft/winget-dsc/refs/heads/main/samples/Configuration%20files/Learn%20tutorials/Windows%20AI/learn_wcr.winget ``` -------------------------------- ### Install Windows ML for C# (Framework-Dependent) Source: https://github.com/microsoftdocs/windows-ai-docs/blob/docs/docs/new-windows-ml/distributing-your-app.md For framework-dependent deployment in C#, install the Microsoft.WindowsAppSDK.ML and Microsoft.WindowsAppSDK.Runtime NuGet packages. Refer to the Windows App SDK deployment documentation for runtime deployment. ```powershell Install-Package Microsoft.WindowsAppSDK.ML Install-Package Microsoft.WindowsAppSDK.Runtime ``` -------------------------------- ### Enable Direct3D 12 Debug Layer Example Source: https://github.com/microsoftdocs/windows-ai-docs/blob/docs/docs/directml/dml-debug-layer.md This is a reference to an example demonstrating how to activate the Direct3D 12 debug layer. Ensure this is enabled before enabling the DirectML debug layer. ```csharp Refer to [ID3D12Debug::EnableDebugLayer](/windows/win32/api/d3d12sdklayers/nf-d3d12sdklayers-id3d12debug-enabledebuglayer) for an example of how to activate the Direct3D 12 debug layer. ``` -------------------------------- ### Get All Installed Execution Providers (C++/WinRT) Source: https://github.com/microsoftdocs/windows-ai-docs/blob/docs/docs/new-windows-ml/register-execution-providers.md Fetches all installed execution providers by iterating through the catalog and filtering out those not present. ```cppwinrt auto catalog = ExecutionProviderCatalog::GetDefault(); // Get all installed execution providers auto allProviders = catalog.FindAllProviders(); std::vector installedProviders; for (auto const& p : allProviders) { if (p.ReadyState() != ExecutionProviderReadyState::NotPresent) { installedProviders.push_back(p); } } ``` -------------------------------- ### Get All Installed Execution Providers (C#) Source: https://github.com/microsoftdocs/windows-ai-docs/blob/docs/docs/new-windows-ml/register-execution-providers.md Retrieves all execution providers installed on the device by checking their ReadyState. Ensure the execution provider is not 'NotPresent'. ```csharp // Get all installed execution providers IEnumerable installedProviders = ExecutionProviderCatalog .GetDefault() .FindAllProviders() .Where(i => i.ReadyState != ExecutionProviderReadyState.NotPresent); ``` -------------------------------- ### Download and Install Execution Provider with Progress (Python) Source: https://github.com/microsoftdocs/windows-ai-docs/blob/docs/docs/new-windows-ml/initialize-execution-providers.md Initiate an EP download with `ensure_ready_async()`. Define a `progress` callback function to handle updates and use `.get()` to await completion. Check the `status` of the result. ```python # Start the download and install of a NotPresent EP operation = provider.ensure_ready_async() # Listen to progress callback def on_progress(async_info, progress_info): # progress_info is out of 100, convert to 0-1 range normalized_progress = progress_info / 100.0 # Display the progress to the user print(f"Progress: {normalized_progress:.0%}") operation.progress = on_progress # Await for the download and install to complete result = operation.get() # Check that the download and install was successful installed = result.status == winml.ExecutionProviderReadyResultState.SUCCESS ``` -------------------------------- ### Download and Install Execution Provider with Progress (C#) Source: https://github.com/microsoftdocs/windows-ai-docs/blob/docs/docs/new-windows-ml/initialize-execution-providers.md Initiate the download of an EP using `EnsureReadyAsync()`. Register a progress callback to receive updates and await the operation's completion. Verify the `Status` of the result. ```csharp // Start the download and install of a NotPresent EP var operation = provider.EnsureReadyAsync(); // Listen to progress callback operation.Progress = (asyncInfo, progressInfo) => { // Dispatch to UI thread (varies based on UI platform) _dispatcherQueue.TryEnqueue(() => { // progressInfo is out of 100, convert to 0-1 range double normalizedProgress = progressInfo / 100.0; // Display the progress to the user Progress = normalizedProgress; }); }; // Await for the download and install to complete var result = await operation; // Check that the download and install was successful bool installed = result.Status == ExecutionProviderReadyResultState.Success; ``` -------------------------------- ### Get All Installed Execution Providers (C/C++) Source: https://github.com/microsoftdocs/windows-ai-docs/blob/docs/docs/new-windows-ml/register-execution-providers.md Collects all installed execution providers using a callback function. It filters out providers that are 'NotPresent'. Requires WinMLEpCatalog.h. ```c #include #include #include struct InstalledProvider { std::string name; WinMLEpHandle handle; }; struct CollectContext { std::vector installedProviders; }; BOOL CALLBACK CollectInstalledCallback( WinMLEpHandle ep, const WinMLEpInfo* info, void* context) { auto* ctx = static_cast(context); if (info && info->name && info->readyState != WinMLEpReadyState_NotPresent) { ctx->installedProviders.push_back({ info->name, ep }); } return TRUE; } // Get all installed execution providers WinMLEpCatalogHandle catalog = nullptr; HRESULT hr = WinMLEpCatalogCreate(&catalog); if (FAILED(hr)) return; CollectContext ctx; WinMLEpCatalogEnumProviders(catalog, CollectInstalledCallback, &ctx); // ctx.installedProviders now contains all installed providers WinMLEpCatalogRelease(catalog); ``` -------------------------------- ### Get All Installed Execution Providers (Python) Source: https://github.com/microsoftdocs/windows-ai-docs/blob/docs/docs/new-windows-ml/register-execution-providers.md Filters all available execution providers to find those that are not in the 'NotPresent' state. ```python catalog = winml.ExecutionProviderCatalog.get_default() # Get all installed execution providers installed_providers = [ p for p in catalog.find_all_providers() if p.ready_state != winml.ExecutionProviderReadyState.NOT_PRESENT ] ``` -------------------------------- ### 1-Channel Padded Fold Operator Example Source: https://github.com/microsoftdocs/windows-ai-docs/blob/docs/docs/directml/api/ns-directml-dml_unfold_operator_desc.md Illustrates the 1-channel fold operator with padding applied to the start and end of dimensions. This operator is available from DML_FEATURE_LEVEL_6_4. ```text InputTensor: (Sizes:{1, 1, 5, 5}, DataType:FLOAT32) [[[[ 0., 1., 2., 3., 4.], [ 5., 6., 7., 8., 9.], [10., 11., 12., 13., 14.], [15., 16., 17., 18., 19.], [20., 21., 22., 23., 24.]]]] DimensionCount: 2 WindowSizes: {3, 3} Strides: {1, 1} Dilations: {1, 1} StartPadding: {1, 0} EndPadding: {1, 0} OutputTensor: (Sizes:{1, 9, 15}, DataType:FLOAT32) [[[ 0., 0., 0., 0., 1., 2., 5., 6., 7., 10., 11., 12., 15., 16., 17.], [ 0., 0., 0., 1., 2., 3., 6., 7., 8., 11., 12., 13., 16., 17., 18.], [ 0., 0., 0., 2., 3., 4., 7., 8., 9., 12., 13., 14., 17., 18., 19.], [ 0., 1., 2., 5., 6., 7., 10., 11., 12., 15., 16., 17., 20., 21., 22.], [ 1., 2., 3., 6., 7., 8., 11., 12., 13., 16., 17., 18., 21., 22., 23.], [ 2., 3., 4., 7., 8., 9., 12., 13., 14., 17., 18., 19., 22., 23., 24.], [ 5., 6., 7., 10., 11., 12., 15., 16., 17., 20., 21., 22., 0., 0., 0.], [ 6., 7., 8., 11., 12., 13., 16., 17., 18., 21., 22., 23., 0., 0., 0.], [ 7., 8., 9., 12., 13., 14., 17., 18., 19., 22., 23., 24., 0., 0., 0.]]] ``` -------------------------------- ### Get all EPs and log their names and versions in C++/WinRT Source: https://github.com/microsoftdocs/windows-ai-docs/blob/docs/docs/new-windows-ml/versioning.md This C++/WinRT code retrieves all compatible execution providers and logs their names and versions. It handles cases where an EP is not installed. ```cppwinrt auto catalog = winrt::Microsoft::Windows::AI::MachineLearning::ExecutionProviderCatalog::GetDefault(); auto providers = catalog.FindAllProviders(); // For each provider for (auto const& provider : providers) { // Log the name OutputDebugString((L"Windows ML EP: " + provider.Name() + L"\n").c_str()); // Log the version auto packageId = provider.PackageId(); if (packageId) { auto v = packageId.Version(); std::wstring versionStr = std::format(L"Version: {}.{}.{}.{}\n", v.Major, v.Minor, v.Build, v.Revision); OutputDebugString(versionStr.c_str()); } else { OutputDebugString(L"Version: Not installed\n"); } } ``` -------------------------------- ### Create a new .NET Console Project Source: https://github.com/microsoftdocs/windows-ai-docs/blob/docs/docs/foundry-local/get-started.md Create a new .NET console application and navigate into its directory. ```bash dotnet new console -n FoundryLocalDemo cd FoundryLocalDemo ``` -------------------------------- ### Get Image Description in C++/WinRT Source: https://github.com/microsoftdocs/windows-ai-docs/blob/docs/docs/apis/image-description.md This C++/WinRT example shows how to obtain a text description for an image. It includes checking model readiness, initializing the ImageDescriptionGenerator, and applying content filters. ```cppwinrt #include #include #include #include #include #include #include #include using namespace winrt::Microsoft::Graphics::Imaging; using namespace winrt::Microsoft::Windows::AI; using namespace winrt::Microsoft::Windows::AI::ContentSafety; using namespace winrt::Microsoft::Windows::AI::Imaging; using namespace winrt::Windows::Foundation; using namespace winrt::Windows::Graphics::Imaging; using namespace winrt::Windows::Storage::Streams; using namespace winrt::Windows::Storage::StorageFile; if (ImageDescriptionGenerator::GetReadyState() == AIFeatureReadyState::NotReady) { auto loadResult = ImageDescriptionGenerator::EnsureReadyAsync().get(); if (loadResult.Status() != AIFeatureReadyResultState::Success) { throw winrt::hresult_error(loadResult.ExtendedError()); } } ImageDescriptionGenerator imageDescriptionGenerator = ImageDescriptionGenerator::CreateAsync().get(); // Convert already available softwareBitmap to ImageBuffer. auto inputBuffer = Microsoft::Graphics::Imaging::ImageBuffer::CreateForSoftwareBitmap(softwareBitmap); // Create content moderation thresholds object. ContentFilterOptions contentFilter{}; contentFilter.PromptMaxAllowedSeverityLevel().Violent(SeverityLevel::Medium); contentFilter.ResponseMaxAllowedSeverityLevel().Violent(SeverityLevel::Medium); // Get text description. auto response = imageDescriptionGenerator.DescribeAsync(inputBuffer, ImageDescriptionKind::BriefDescription, contentFilter).get(); string text = response.Description(); ``` -------------------------------- ### Generate Structured JSON Response Source: https://github.com/microsoftdocs/windows-ai-docs/blob/docs/docs/apis/phi-silica-structured-output.md This example demonstrates how to call GenerateStructuredJsonResponseAsync to get a JSON response constrained by a schema. Ensure the model is ready and handle potential invalid JSON responses. ```csharp // 1. Ensure the language model is ready. var readyState = await languageModel.GetReadyStateAsync(); if (readyState != LanguageModelReadyState.Ready) { await languageModel.EnsureReadyAsync(); } // 2. Wrap the LanguageModel in LanguageModelExperimental. var languageModelExperimental = new LanguageModelExperimental(languageModel); // 3. Define your JSON Schema as a raw JSON string. string personSchema = @"{ \"type\": \"object\", \"properties\": { \"name\": { \"type\": \"string\" }, \"age\": { \"type\": \"integer\" } }, \"required\": [\"name\", \"age\"] }"; // 4. Call GenerateStructuredJsonResponseAsync. var result = await languageModelExperimental.GenerateStructuredJsonResponseAsync( prompt: "Create a person object with name and age.", schema: personSchema, options: new LanguageModelOptions() ); // 5. Check the result status. if (result.Status == LanguageModelResponseStatus.Complete) { // The JSON output is valid and conforms to the schema. Console.WriteLine(result.Text); } else if (result.Status == LanguageModelResponseStatus.ResponseInvalidJson) { // The model produced text that didn't conform to the schema. Console.WriteLine($"Invalid JSON generated: {result.Text}"); } else { // Handle other potential errors. Console.WriteLine($"An error occurred: {result.Status}"); } ``` -------------------------------- ### Complete Model Catalog Integration Example Source: https://github.com/microsoftdocs/windows-ai-docs/blob/docs/docs/new-windows-ml/model-catalog/get-started.md This snippet demonstrates the end-to-end process of creating a Model Catalog, finding a specific model, checking its details, downloading it if necessary, and obtaining its local path for inference. ```csharp using Microsoft.Windows.AI.MachineLearning; using System; using System.Threading.Tasks; try { // Create catalog with source var source = await ModelCatalogSource.CreateFromUriAsync( new Uri("https://contoso.com/models")); var catalog = new ModelCatalog(new ModelCatalogSource[] { source }); // Find a model Console.WriteLine("Searching for model..."); CatalogModelInfo model = await catalog.FindModelAsync("phi-3.5-reasoning"); if (model != null) { Console.WriteLine($"Found model: {model.Name}"); Console.WriteLine($"Version: {model.Version}"); Console.WriteLine($"Publisher: {model.Publisher}"); Console.WriteLine($"Size: {model.ModelSizeInBytes / (1024 * 1024)} MB"); Console.WriteLine($"Supported execution providers: {string.Join(", ", model.ExecutionProviders)}"); // Get an instance of the model (downloads if necessary) var progress = new Progress(percent => Console.WriteLine($"Download progress: {percent:P}")); CatalogModelInstanceResult result = await model.GetInstanceAsync().AsTask(progress); if (result.Status == CatalogModelInstanceStatus.Available) { CatalogModelInstance instance = result.GetInstance(); // Get the model path string modelPath = instance.ModelPaths[0]; Console.WriteLine($"Model path: {modelPath}"); // Inference your model using your own code } else { Console.WriteLine($"Failed to get model: {result.ExtendedError}"); Console.WriteLine($"Details: {result.DiagnosticText}"); } } else { Console.WriteLine("Model not found"); } } catch (Exception ex) { Console.WriteLine($"Error: {ex.Message}"); } ``` -------------------------------- ### Initialize DirectML Device and Tensors Source: https://github.com/microsoftdocs/windows-ai-docs/blob/docs/docs/directml/pytorch-windows.md Import necessary libraries, initialize the DirectML device, and create two sample tensors. Ensure the tensors are moved to the 'dml' device for computation. ```python import torch import torch_directml dml = torch_directml.device() tensor1 = torch.tensor([1]).to(dml) tensor2 = torch.tensor([2]).to(dml) ``` -------------------------------- ### Build MCP Bundle using C# Sample Source: https://github.com/microsoftdocs/windows-ai-docs/blob/docs/docs/mcp/servers/mcp-mcpb.md Clones the MCP server C# sample repository, navigates to the directory, and executes a PowerShell script to build the MCP bundle. This process generates an .mcpb file. ```powershell git clone https://github.com/microsoft/mcp-on-windows-samples.git cd mcp-on-windows-samples cd mcp-server-csharp .\build-mcpb.ps1 ``` -------------------------------- ### C++: Registering Custom Execution Providers Source: https://github.com/microsoftdocs/windows-ai-docs/blob/docs/docs/new-windows-ml/initialize-execution-providers.md This C++ example demonstrates how to enumerate, download, and register custom execution providers with ONNX Runtime. It checks for providers like VitisAI, OpenVINO, QNN, and NvTensorRT RTX, prompts the user for download if necessary, and then registers the ready providers. ```cpp #include #include #include #include #include // List of provider names our app supports const char* targetProviderNames[] = { "VitisAIExecutionProvider", "OpenVINOExecutionProvider", "QNNExecutionProvider", "NvTensorRtRtxExecutionProvider" }; const size_t targetProviderCount = sizeof(targetProviderNames) / sizeof(targetProviderNames[0]); bool IsTargetProvider(const char* name) { for (size_t i = 0; i < targetProviderCount; i++) { if (strcmp(name, targetProviderNames[i]) == 0) return true; } return false; } // Context for enumeration callbacks struct ProductionContext { bool needsDownload; bool userWantsToDownload; Ort::Env* env; }; // Check if any target providers need downloading BOOL CALLBACK CheckTargetProvidersCallback( WinMLEpHandle ep, const WinMLEpInfo* info, void* context) { if (info == nullptr || info->name == nullptr) return TRUE; // Skip invalid entries ProductionContext* ctx = static_cast(context); if (IsTargetProvider(info->name) && info->readyState == WinMLEpReadyState_NotPresent) { ctx->needsDownload = true; } return TRUE; // Continue to check all providers } // Download missing and register ready target providers BOOL CALLBACK ProcessTargetProvidersCallback( WinMLEpHandle ep, const WinMLEpInfo* info, void* context) { if (info == nullptr || info->name == nullptr) return TRUE; // Skip invalid entries ProductionContext* ctx = static_cast(context); if (!IsTargetProvider(info->name)) return TRUE; // Skip non-target providers // Download if user agreed and provider is not present if (ctx->userWantsToDownload && info->readyState == WinMLEpReadyState_NotPresent) { WinMLEpEnsureReady(ep); } // Re-check state and register if ready WinMLEpReadyState currentState; WinMLEpGetReadyState(ep, ¤tState); if (currentState == WinMLEpReadyState_Ready) { // Get the library path size_t pathSize = 0; WinMLEpGetLibraryPathSize(ep, &pathSize); std::string libraryPathUtf8(pathSize, '\0'); WinMLEpGetLibraryPath(ep, pathSize, libraryPathUtf8.data(), nullptr); // Register with ONNX Runtime std::filesystem::path libraryPath(libraryPathUtf8); ctx->env->RegisterExecutionProviderLibrary(info->name, libraryPath.wstring()); } return TRUE; // Continue to check all providers } void ProductionAppExample(Ort::Env& env, bool userWantsToDownload) { WinMLEpCatalogHandle catalog = nullptr; HRESULT hr = WinMLEpCatalogCreate(&catalog); if (FAILED(hr)) return; ProductionContext ctx = { false, userWantsToDownload, &env }; // First pass: check if any target providers need downloading WinMLEpCatalogEnumProviders(catalog, CheckTargetProvidersCallback, &ctx); if (ctx.needsDownload && !userWantsToDownload) { // TODO: Show UI to user asking if they want to download // ctx.userWantsToDownload = ShowDownloadDialog(); } // Second pass: download (if requested) and register target providers WinMLEpCatalogEnumProviders(catalog, ProcessTargetProvidersCallback, &ctx); WinMLEpCatalogRelease(catalog); } ``` -------------------------------- ### Find All Available EPs (C#) Source: https://github.com/microsoftdocs/windows-ai-docs/blob/docs/docs/new-windows-ml/initialize-execution-providers.md This C# snippet demonstrates how to discover all EPs available to the user's device, including those not yet installed. It iterates through the found providers and prints their names and readiness states. ```csharp using Microsoft.Windows.AI.MachineLearning; using System; ExecutionProviderCatalog catalog = ExecutionProviderCatalog.GetDefault(); // Find all available EPs (including non-installed EPs) ExecutionProvider[] providers = catalog.FindAllProviders(); foreach (var provider in providers) { Console.WriteLine($"{provider.Name}: {provider.ReadyState}"); } ``` -------------------------------- ### Install PyTorch with DirectML Source: https://github.com/microsoftdocs/windows-ai-docs/blob/docs/docs/directml/pytorch-windows.md Install the torch-directml package using pip. This command installs the latest release, enabling PyTorch to use DirectML for hardware acceleration. ```bash pip install torch-directml ``` -------------------------------- ### Register an Installed Provider (C/C++) Source: https://github.com/microsoftdocs/windows-ai-docs/blob/docs/docs/new-windows-ml/register-execution-providers.md Prepares an execution provider using WinMLEpEnsureReady, retrieves its library path, and registers it with ONNX Runtime's Ort::Env. Assumes an Ort::Env object is already created. ```c #include #include #include #include // Assumes an Ort::Env has already been created // Ort::Env env(ORT_LOGGING_LEVEL_ERROR, "MyApp"); // Prepare the provider (download if necessary) HRESULT hr = WinMLEpEnsureReady(ep); if (SUCCEEDED(hr)) { // Get the library path size_t pathSize = 0; WinMLEpGetLibraryPathSize(ep, &pathSize); std::string libraryPathUtf8(pathSize, '\0'); WinMLEpGetLibraryPath(ep, pathSize, libraryPathUtf8.data(), nullptr); // Register it with ONNX Runtime std::filesystem::path libraryPath(libraryPathUtf8); env.RegisterExecutionProviderLibrary(providerName, libraryPath.wstring()); } ``` -------------------------------- ### Install Windows ML for Python (Framework-Dependent) Source: https://github.com/microsoftdocs/windows-ai-docs/blob/docs/docs/new-windows-ml/distributing-your-app.md For framework-dependent deployment in Python, install the specified pip packages and the matching Windows App SDK Runtime. Ensure your Python installation is unpackaged. ```powershell pip install wasdk-Microsoft.Windows.AI.MachineLearning[all] wasdk-Microsoft.Windows.ApplicationModel.DynamicDependency.Bootstrap onnxruntime-windowsml ``` -------------------------------- ### Initialize Windows ML Environment and Execution Providers (C#) Source: https://github.com/microsoftdocs/windows-ai-docs/blob/docs/docs/new-windows-ml/tutorial.md Creates a Windows ML environment with specified logging options and ensures necessary execution providers are downloaded and registered. This snippet is useful for setting up the core components for running ML models on Windows. ```csharp using Microsoft.AI.Skills.Services; using Microsoft.AI.Skills.Services.Common; using Microsoft.AI.Skills.Services.Native; using System; using System.Threading.Tasks; // Create a new instance of EnvironmentCreationOptions EnvironmentCreationOptions envOptions = new() { logId = "ResnetDemo", logLevel = OrtLoggingLevel.ORT_LOGGING_LEVEL_ERROR }; // Pass the options by reference to CreateInstanceWithOptions OrtEnv ortEnv = OrtEnv.CreateInstanceWithOptions(ref envOptions); // Use Windows ML to download and register Execution Providers var catalog = Microsoft.Windows.AI.MachineLearning.ExecutionProviderCatalog.GetDefault(); Console.WriteLine("Ensuring and registering execution providers..."); await catalog.EnsureAndRegisterCertifiedAsync(); //Create Onnx session Console.WriteLine("Creating session ..."); var sessionOptions = new SessionOptions(); // Set EP Selection Policy sessionOptions.SetEpSelectionPolicy(ExecutionProviderDevicePolicy.MIN_OVERALL_POWER); ``` -------------------------------- ### C# Example: Conditionally Download and Register Execution Providers Source: https://github.com/microsoftdocs/windows-ai-docs/blob/docs/docs/new-windows-ml/initialize-execution-providers.md This C# snippet demonstrates how to check for available execution providers, prompt the user for download, and then register them. It filters providers by name and handles cases where providers are not present. ```csharp using Microsoft.Windows.AI.MachineLearning; var catalog = ExecutionProviderCatalog.GetDefault(); // Filter to the EPs our app supports/uses var providers = catalog.FindAllProviders().Where(p => p.Name == "MIGraphXExecutionProvider" || p.Name == "VitisAIExecutionProvider" || p.Name == "OpenVINOExecutionProvider" || p.Name == "QNNExecutionProvider" || p.Name == "NvTensorRtRtxExecutionProvider" ); if (providers.Any(p => p.ReadyState == ExecutionProviderReadyState.NotPresent)) { // Show UI to user asking if they want to download new execution providers bool userWantsToDownload = await ShowDownloadDialogAsync(); if (userWantsToDownload) { // Download all EPs foreach (var p in providers) { if (p.ReadyState == ExecutionProviderReadyState.NotPresent) { // Ignore result handling here; production code could inspect status await p.EnsureReadyAsync(); } } // And register all EPs await catalog.RegisterCertifiedAsync(); } else { // Register only already-present EPs await catalog.RegisterCertifiedAsync(); } } ``` -------------------------------- ### Install Dependencies for C++ App Actions Source: https://github.com/microsoftdocs/windows-ai-docs/blob/docs/docs/app-actions/actions-uri-launch.md Use this command to automatically install necessary dependencies for C++ App Actions development, including enabling Developer Mode and installing Visual Studio with relevant workloads. ```console winget configure https://raw.githubusercontent.com/microsoft/winget-dsc/refs/heads/main/samples/Configuration%20files/Learn%20tutorials/Windows%20AI/app_actions_cpp.winget ``` -------------------------------- ### Install Dependencies for C# App Actions Source: https://github.com/microsoftdocs/windows-ai-docs/blob/docs/docs/app-actions/actions-uri-launch.md Use this command to automatically install necessary dependencies for C# App Actions development, including enabling Developer Mode and installing Visual Studio with relevant workloads. ```console winget configure https://raw.githubusercontent.com/microsoft/winget-dsc/refs/heads/main/samples/Configuration%20files/Learn%20tutorials/Windows%20AI/app_actions_cs.winget ``` -------------------------------- ### Example Agent Definition JSON Source: https://github.com/microsoftdocs/windows-ai-docs/blob/docs/docs/agent-launchers/agents-json.md Provides an example of a complete agent definition JSON file. ```APIDOC ## Example Agent Definition JSON File ```json { "manifest_version": "0.1.0", "version": "1.0.0", "name": "Zava.ZavaAgent", "display_name": "ms-resource://zavaAgentDisplayName", "description": "ms-resource://zavaAgentDescription", "placeholder_text": "ms-resource://zavaAgentPlaceHolderText", "icon": "ms-resource://Files/Assets/ZavaLogo.png", "action_id": "ZavaAgentAction" } ``` ``` -------------------------------- ### Initialize Catalog with a Single URI Source Source: https://github.com/microsoftdocs/windows-ai-docs/blob/docs/docs/new-windows-ml/model-catalog/get-started.md Create a catalog source from a URI and initialize the ModelCatalog with it. Ensure the URI points to a valid model catalog source. ```csharp using Microsoft.Windows.AI.MachineLearning; using Windows.Foundation; using System; using System.Threading.Tasks; // Create a catalog source from a URI var source = await ModelCatalogSource.CreateFromUriAsync( new Uri("https://contoso.com/models")); // Create the catalog with the source var catalog = new ModelCatalog(new ModelCatalogSource[] { source }); ``` -------------------------------- ### Install Windows ML for C# (Self-Contained) Source: https://github.com/microsoftdocs/windows-ai-docs/blob/docs/docs/new-windows-ml/distributing-your-app.md For self-contained deployment in C#, targeting Windows 10 Build 18362 or higher, install Microsoft.Windows.AI.MachineLearning. For older builds (17763+), use Microsoft.WindowsAppSDK.ML. Do not install Microsoft.WindowsAppSDK.Runtime or the main Microsoft.WindowsAppSDK package. ```powershell Install-Package Microsoft.Windows.AI.MachineLearning # Or for older builds: Install-Package Microsoft.WindowsAppSDK.ML ``` -------------------------------- ### Create and Run a Python Application Source: https://github.com/microsoftdocs/windows-ai-docs/blob/docs/docs/foundry-local/get-started.md This Python script initializes the Foundry Local manager, downloads and loads a specified model, and then uses its chat client to get a response to a user query. It also demonstrates how to unload the model after use. ```python from foundry_local_sdk import Configuration, FoundryLocalManager FoundryLocalManager.initialize(Configuration(app_name="my-app")) manager = FoundryLocalManager.instance model = manager.catalog.get_model("qwen2.5-0.5b") model.download(lambda p: print(f"\rDownloading {p:.0f}%", end="", flush=True)) model.load() client = model.get_chat_client() for chunk in client.complete_streaming_chat([{"role": "user", "content": "Why is the sky blue?"}]): print(chunk.choices[0].delta.content or "", end="", flush=True) print() model.unload() ``` ```bash python app.py ``` -------------------------------- ### MCPB Config JSON File Example Source: https://github.com/microsoftdocs/windows-ai-docs/blob/docs/docs/mcp/servers/mcp-windows-identity.md This JSON file defines the metadata, tools, and server configuration for an MCP bundle. Ensure the _meta section matches runtime responses for default mode operation. ```json { "manifest_version": "0.1", "name": "MCP-Server-Sample-App", "version": "1.0.0", "description": "A Sample App MCP Server", "author": { "name": "Microsoft" }, "server": { "type": "binary", "entry_point": "SampleMCPServer.McpServer.exe", "mcp_config": { "command": "SampleMCPServer.McpServer.exe", "args": [] } }, "tools": [ { "name": "get_random_fact", "description": "Gets a random interesting fact from an online API." }, { "name": "get_random_quote", "description": "Gets a random inspirational quote from an online API." }, { "name": "get_weather", "description": "Gets current weather information for a specified city." } ], "tools_generated": false, "license": "MIT", "_meta": { "com.microsoft.windows": { "static_responses": { "initialize": { "protocolVersion": "2025-06-18", "capabilities": { "logging": {}, "tools": { "listChanged": true } }, "serverInfo": { "name": "McpServer", "version": "1.0.0.0" } }, "tools/list": { "tools": [ { "name": "get_random_quote", "description": "Gets a random inspirational quote from an online API.", "inputSchema": { "type": "object", "properties": {} } }, { "name": "get_random_fact", "description": "Gets a random interesting fact from an online API.", "inputSchema": { "type": "object", "properties": {} } }, { "name": "get_weather", "description": "Gets current weather information for a specified city.", "inputSchema": { "type": "object", "properties": { "city": { "type": "string", "default": "London" } } } } ] } } } } } ``` -------------------------------- ### Verify Foundry Local CLI Installation Source: https://github.com/microsoftdocs/windows-ai-docs/blob/docs/docs/foundry-local/get-started.md Verify that the Foundry Local CLI has been installed correctly by checking its version. ```bash foundry --version ``` -------------------------------- ### Example Action Definition JSON Source: https://github.com/microsoftdocs/windows-ai-docs/blob/docs/docs/app-actions/actions-json.md This JSON defines two sample actions: Contoso.SampleGreeting and Contoso.SampleGetText. It includes details like action ID, description, icon, input parameters, and invocation methods (URI and COM). ```json "version": 3, "actions": [ { "id": "Contoso.SampleGreeting", "description": "Send greeting with Contoso", "icon": "ms-resource//...", "displaysUI": false, "usesGenerativeAI": false, "isAvailable": false, "allowedAppInvokers": ["*"], "inputs": [ { "name": "UserFriendlyName", "kind": "Text" }, { "name": "PetName", "kind": "Text", "required": false } ], "inputCombinations": [ { "inputs": ["UserFriendlyName"], "description": "Greet ${UserFriendlyName.Text}" }, { "inputs": ["UserFriendlyName", "PetName"], "description": "Greet ${UserFriendlyName.Text} and their pet ${PetName.Text}" } ], "contentAgeRating": "child", "invocation": { { "type": "Uri", "uri": "contoso://greetUser?userName=${UserFriendlyName.Text}&petName=${PetName.Text}", }, "where": [ "${UserFriendlyName.Length > 3}" ] } }, { "id": "Contoso.SampleGetText", "description": "Summarize file with Contoso", "icon": "ms-resource://...", "inputs": [ { "name": "FileToSummarize", "kind": "File" } ], "inputCombinations": [ { "inputs": ["FileToSummarize"], "description": "Summarize ${FileToSummarize.Path}" }, ], "outputs": [ { "name": "Summary", "kind": "Text" } ], "contentAgeRating": "child", "invocation": { "type": "COM", "clsid": "{...}" } } ] } ``` -------------------------------- ### Initialize DirectML Device and Tensors Source: https://github.com/microsoftdocs/windows-ai-docs/blob/docs/docs/directml/pytorch-wsl.md Import PyTorch and torch-directml, then initialize the DirectML device. Create two tensors and move them to the DirectML device for computation. ```python import torch import torch_directml dml = torch_directml.device() tensor1 = torch.tensor([1]).to(dml) tensor2 = torch.tensor([2]).to(dml) ``` -------------------------------- ### Install Miniconda Source: https://github.com/microsoftdocs/windows-ai-docs/blob/docs/docs/directml/pytorch-wsl.md Download and install the Miniconda package for Linux within WSL 2. This sets up a Python environment manager. ```bash wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh bash Miniconda3-latest-Linux-x86_64.sh ``` -------------------------------- ### Initialize Windows ML Execution Providers Source: https://github.com/microsoftdocs/windows-ai-docs/blob/docs/docs/new-windows-ml/initialize-execution-providers.md This snippet demonstrates how to initialize the Windows ML execution providers, filter for supported EPs, and handle missing installations. It ensures that all necessary EPs are ready before proceeding. ```python from pathlib import Path from importlib import metadata site_packages_path = Path(str(metadata.distribution('winrt-runtime').locate_file(''))) dll_path = site_packages_path / 'winrt' / 'msvcp140.dll' if dll_path.exists(): dll_path.unlink() from winui3.microsoft.windows.applicationmodel.dynamicdependency.bootstrap import ( InitializeOptions, initialize ) import winui3.microsoft.windows.ai.machinelearning as winml import onnxruntime as ort with initialize(options=InitializeOptions.ON_NO_MATCH_SHOW_UI): catalog = winml.ExecutionProviderCatalog.get_default() # Filter EPs that the app supports providers = [provider for provider in catalog.find_all_providers() if provider.name in [ 'VitisAIExecutionProvider', 'OpenVINOExecutionProvider', 'QNNExecutionProvider', 'NvTensorRtRtxExecutionProvider' ]] # Download and make ready missing EPs if the user wants to if any(provider.ready_state == winml.ExecutionProviderReadyState.NOT_PRESENT for provider in providers): # Ask the user if they want to download the missing packages if user_wants_to_download: for provider in [provider for provider in providers if provider.ready_state == winml.ExecutionProviderReadyState.NOT_PRESENT]: provider.ensure_ready_async().get() # Make ready the existing EPs for provider in [provider for provider in providers if provider.ready_state == winml.ExecutionProviderReadyState.NOT_READY]: provider.ensure_ready_async().get() # Register all ready EPs for provider in [provider for provider in providers if provider.ready_state == winml.ExecutionProviderReadyState.READY]: ort.register_execution_provider_library(provider.name, provider.library_path) ``` -------------------------------- ### Install NodeJS using WinGet Source: https://github.com/microsoftdocs/windows-ai-docs/blob/docs/docs/mcp/servers/test-mcp-server.md Install NodeJS on your device using the WinGet package manager. This is a prerequisite for using the MCP inspector. ```powershell winget install OpenJS.NodeJS ```