-
-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathGetComponentSamplesTool.cs
More file actions
63 lines (52 loc) · 2.23 KB
/
GetComponentSamplesTool.cs
File metadata and controls
63 lines (52 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System.ComponentModel;
using System.Diagnostics;
using System.Text;
using Havit.Blazor.Documentation.Mcp.Diagnostics;
using Havit.Blazor.Documentation.Services;
using ModelContextProtocol.Server;
namespace Havit.Blazor.Documentation.Mcp.Tools;
/// <summary>
/// MCP tool that returns component demo samples from embedded resources.
/// </summary>
internal class GetComponentSamplesTool
{
private readonly IComponentDemosProvider _componentDemosProvider;
public GetComponentSamplesTool(IComponentDemosProvider componentDemosProvider)
{
_componentDemosProvider = componentDemosProvider;
}
/// <summary>
/// Returns all demo samples for a given HAVIT Blazor component in Markdown format.
/// </summary>
[McpServerTool(Name = "get_component_samples", Title = "Get component samples", Destructive = false, Idempotent = true, ReadOnly = true)]
[Description("Returns all demo/sample code snippets (Razor source) for a HAVIT Blazor component. Provide the component name, e.g. 'HxButton', 'HxGrid', 'HxInputText'.")]
public string GetComponentSamples(
[Description("Name of the component to get samples for, e.g. 'HxButton', 'HxGrid', 'HxInputText'.")] string componentName)
{
using Activity activity = McpToolActivitySource.Source.StartActivity("get_component_samples");
activity?.SetTag("mcp.tool.parameter.componentName", componentName);
IReadOnlyList<string> resourceNames = _componentDemosProvider.GetComponentDemoResourceNames(componentName);
if (resourceNames.Count == 0)
{
activity?.SetTag("mcp.tool.result", "not_found");
return $"No demo samples found for component '{componentName}'.";
}
activity?.SetTag("mcp.tool.result", "success");
activity?.SetTag("mcp.tool.sampleCount", resourceNames.Count);
StringBuilder sb = new StringBuilder();
sb.AppendLine($"# {componentName} — Demo Samples");
sb.AppendLine();
foreach (string resourceName in resourceNames)
{
string fileName = ComponentDemosProvider.ExtractFileName(resourceName);
string content = _componentDemosProvider.GetDemoContentByResourceName(resourceName);
sb.AppendLine($"## {fileName}");
sb.AppendLine();
sb.AppendLine("```razor");
sb.AppendLine(content);
sb.AppendLine("```");
sb.AppendLine();
}
return sb.ToString();
}
}