-
Notifications
You must be signed in to change notification settings - Fork 11
Adding a command to generate MCP server package for submission on Mic… #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -138,5 +138,55 @@ public static string[] GetAllScopes() | |
| return ServerToScope.Values.Select(v => v.Scope).Distinct().OrderBy(s => s).ToArray(); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| // PackageMCPServer constants | ||
| public static class PackageMCPServer | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you move this to its own file? |
||
| { | ||
| public const string OutlinePngIconFileName = "outline.png"; | ||
| public const string ColorPngIconFileName = "color.png"; | ||
| public const string ManifestFileName = "manifest.json"; | ||
| public const string TemplateManifestJson = | ||
| @" | ||
| { | ||
| ""$schema"": ""https://developer.microsoft.com/en-us/json-schemas/teams/vDevPreview/MicrosoftTeams.schema.json"", | ||
| ""manifestVersion"": ""devPreview"", | ||
| ""agentConnectors"": [ | ||
| { | ||
| ""id"": ""11111111-1111-1111-1111-111111111112"", | ||
| ""displayName"": ""DUMMY_DISPLAY_NAME"", | ||
| ""description"": ""DUMMY_DESCRIPTION"", | ||
| ""toolSource"": { | ||
| ""remoteMcpServer"": { | ||
| ""mcpServerUrl"": ""https://example.com/mcpServer"", | ||
| ""authorization"": { | ||
| ""type"": ""None"" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ], | ||
| ""version"": ""1.0.0"", | ||
| ""id"": ""11111111-1111-1111-1111-111111111112"", | ||
| ""developer"": { | ||
| ""name"": ""DUMMY_DEVELOPER"", | ||
| ""websiteUrl"": ""https://go.microsoft.com/fwlink/?linkid=2138949"", | ||
| ""privacyUrl"": ""https://go.microsoft.com/fwlink/?linkid=2138865"", | ||
| ""termsOfUseUrl"": ""https://go.microsoft.com/fwlink/?linkid=2138950"" | ||
| }, | ||
| ""name"": { | ||
| ""short"": ""DUMMY_SHORT_NAME"", | ||
| ""full"": ""DUMMY_FULL_NAME"" | ||
| }, | ||
| ""description"": { | ||
| ""short"": ""DUMMY_SHORT_DESCRIPTION"", | ||
| ""full"": ""DUMMY_FULL_DESCRIPTION"" | ||
| }, | ||
| ""icons"": { | ||
| ""outline"": ""outline.png"", | ||
| ""color"": ""color.png"" | ||
| }, | ||
| ""accentColor"": ""#E0F6FC"" | ||
| }"; | ||
| } | ||
|
|
||
| } | ||
178 changes: 178 additions & 0 deletions
178
src/Microsoft.Agents.A365.DevTools.Cli/Helpers/PackageMCPServerHelper.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,178 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO.Compression; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Text.Json; | ||
| using System.Text.Json.Nodes; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Agents.A365.DevTools.Cli.Constants; | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace Microsoft.Agents.A365.DevTools.Cli.Helpers | ||
| { | ||
| public class PackageMCPServerHelper | ||
| { | ||
| /// <summary> | ||
| /// Generates a manifest JSON for an MCP server package. | ||
| /// </summary> | ||
| /// <param name="p">Server information to include in the manifest</param> | ||
| /// <param name="developerName">Name of the developer/publisher</param> | ||
| /// <param name="logger">Logger</param> | ||
| /// <returns>JSON string containing the manifest</returns> | ||
| public static string GenerateManifestJson(ServerInfo p, string developerName, ILogger logger) | ||
| { | ||
nagupta123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| JsonNode root; | ||
| try | ||
| { | ||
| root = JsonNode.Parse(McpConstants.PackageMCPServer.TemplateManifestJson) ?? new JsonObject(); | ||
| } | ||
| catch | ||
nagupta123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| root = new JsonObject(); | ||
| } | ||
|
|
||
| var obj = root as JsonObject ?? new JsonObject(); | ||
|
|
||
| var displayName = p.McpServerDisplayName ?? string.Empty; | ||
| var shortDisplayName = displayName.Length <= 30 ? displayName : displayName.Substring(0, 30); | ||
| var fullDisplayName = displayName.Length <= 100 ? displayName : displayName.Substring(0, 100); | ||
| if (displayName.Length > 30) | ||
| { | ||
| logger.LogWarning("Short name truncated to 30 characters. Original '{Original}' -> '{Short}'", | ||
| displayName, shortDisplayName); | ||
| } | ||
| if (displayName.Length > 100) | ||
| { | ||
| logger.LogWarning("Full name truncated to 100 characters. Original '{Original}' -> '{Full}'", | ||
| displayName, fullDisplayName); | ||
| } | ||
|
|
||
| var description = p.McpServerDescription ?? string.Empty; | ||
| var shortDescription = description.Length <= 80 ? description : description.Substring(0, 80); | ||
| if (description.Length > 80) | ||
| { | ||
| logger.LogWarning("Short description truncated to 80 characters. Original '{Original}' -> '{Short}'", | ||
| description, shortDescription); | ||
| } | ||
|
|
||
| // Replace values. | ||
| if (obj["agentConnectors"] is JsonArray connectors && connectors.Count > 0 && connectors[0] is JsonObject c0) | ||
| { | ||
| Set(c0, "id", p.McpServerId); | ||
| Set(c0, "displayName", shortDisplayName); | ||
| Set(c0, "description", description); | ||
|
|
||
| if (c0["toolSource"]?["remoteMcpServer"] is JsonObject rs) | ||
| { | ||
| Set(rs, "mcpServerUrl", p.McpServerUrl); | ||
| } | ||
| } | ||
| Set(obj, "id", p.McpServerId); | ||
| var developerObj = obj["developer"] as JsonObject ?? (JsonObject)(obj["developer"] = new JsonObject()); | ||
| var nameObj = obj["name"] as JsonObject ?? (JsonObject)(obj["name"] = new JsonObject()); | ||
| var descriptionObj = obj["description"] as JsonObject ?? (JsonObject)(obj["description"] = new JsonObject()); | ||
|
|
||
| Set(developerObj, "name", developerName); | ||
| Set(nameObj, "short", shortDisplayName); | ||
| Set(nameObj, "full", fullDisplayName); | ||
| Set(descriptionObj, "short", shortDescription); | ||
| Set(descriptionObj, "full", description); | ||
|
|
||
| return obj.ToJsonString(new JsonSerializerOptions { WriteIndented = true }); | ||
|
|
||
| static void Set(JsonNode? parent, string prop, string? value) | ||
| { | ||
| if (parent is JsonObject o) | ||
| { | ||
| o[prop] = value ?? string.Empty; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// The method to build the MCP package as a zip file. | ||
| /// </summary> | ||
nagupta123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// <param name="manifestJson">JSON content for the manifest.json file</param> | ||
| /// <param name="info">Server information used for package naming</param> | ||
| /// <param name="iconUrl">Public URL to download the icon from</param> | ||
| /// <param name="outputPath">Directory where the ZIP package will be created</param> | ||
| /// <returns>Full path to the created ZIP file</returns> | ||
| public static string BuildPackage(string manifestJson, ServerInfo info, string iconUrl, string outputPath) | ||
| { | ||
| Directory.CreateDirectory(outputPath); | ||
|
|
||
| // Derive package name from server id | ||
| var baseName = "Package_" + info.McpServerId; | ||
|
|
||
| // Basic sanitization for file name. | ||
| var invalidChars = Path.GetInvalidFileNameChars(); | ||
| var sb = new System.Text.StringBuilder(baseName.Length); | ||
| foreach (var ch in baseName) | ||
| { | ||
| sb.Append(invalidChars.Contains(ch) ? '_' : ch); | ||
| } | ||
| var safeName = sb.ToString(); | ||
| var zipFilePath = Path.Combine(outputPath, $"{safeName}.zip"); | ||
|
|
||
| // Download icon (both outline.png and color.png will use same bytes) | ||
| byte[] iconBytes; | ||
| using (var httpClient = new HttpClient()) | ||
| { | ||
| using var response = httpClient.GetAsync(iconUrl, HttpCompletionOption.ResponseHeadersRead).GetAwaiter().GetResult(); | ||
nagupta123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (!response.IsSuccessStatusCode) | ||
| { | ||
| throw new InvalidOperationException($"Failed to download icon from '{iconUrl}'. HTTP {(int)response.StatusCode} {response.ReasonPhrase}"); | ||
| } | ||
|
|
||
| iconBytes = response.Content.ReadAsByteArrayAsync().GetAwaiter().GetResult(); | ||
nagupta123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (iconBytes.Length == 0) | ||
| { | ||
| throw new InvalidOperationException($"Downloaded icon from '{iconUrl}' is empty."); | ||
| } | ||
| } | ||
nagupta123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| using (var fileStream = new FileStream(zipFilePath, FileMode.Create, FileAccess.Write, FileShare.None)) | ||
| { | ||
| using (var zipArchive = new ZipArchive(fileStream, ZipArchiveMode.Create, leaveOpen: false)) | ||
| { | ||
| // manifest.json | ||
| WriteTextToArchive(zipArchive, McpConstants.PackageMCPServer.ManifestFileName, manifestJson); | ||
|
|
||
| // Icons | ||
| WriteByteToArchive(zipArchive, McpConstants.PackageMCPServer.OutlinePngIconFileName, iconBytes); | ||
| WriteByteToArchive(zipArchive, McpConstants.PackageMCPServer.ColorPngIconFileName, iconBytes); | ||
| } | ||
| } | ||
|
|
||
| return zipFilePath; | ||
| } | ||
|
|
||
| private static void WriteTextToArchive(ZipArchive zipArchive, string fileName, string text) | ||
| { | ||
| var entry = zipArchive.CreateEntry(fileName); | ||
| using var entryStream = entry.Open(); | ||
| using var writer = new StreamWriter(entryStream); | ||
| writer.Write(text); | ||
| } | ||
|
|
||
| private static void WriteByteToArchive(ZipArchive zipArchive, string fileName, byte[] binary) | ||
| { | ||
| var entry = zipArchive.CreateEntry(fileName); | ||
| using var entryStream = entry.Open(); | ||
| using var bw = new BinaryWriter(entryStream); | ||
| bw.Write(binary); | ||
| } | ||
|
|
||
| public sealed class ServerInfo | ||
| { | ||
| public string McpServerId { get; init; } = string.Empty; | ||
| public string McpServerDisplayName { get; init; } = string.Empty; | ||
| public string McpServerDescription { get; init; } = string.Empty; | ||
| public string McpServerUrl { get; init; } = string.Empty; | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.