-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
85 lines (66 loc) · 2.48 KB
/
Program.cs
File metadata and controls
85 lines (66 loc) · 2.48 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.PowerPlatform.Dataverse.Client;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using ModelContextProtocol.Server;
using System.ComponentModel;
var builder = Host.CreateApplicationBuilder(args);
builder.Logging.AddConsole(consoleLogOptions =>
{
// Configure all logs to go to stderr
consoleLogOptions.LogToStandardErrorThreshold = LogLevel.Trace;
});
builder.Services.AddSingleton<IOrganizationService>(provider =>
{
// TODO Enter your Dataverse environment's URL and logon info.
string url = "[URL to your environment]";
string connectionString = $@"
AuthType = ClientSecret;
Url = {url};
ClientId = [ClientId];
Secret = [ClientSecret]";
return new ServiceClient(connectionString);
});
builder.Services
.AddMcpServer()
.WithStdioServerTransport()
.WithToolsFromAssembly();
await builder.Build().RunAsync();
[McpServerToolType]
public static class DataverseTool
{
[McpServerTool, Description("Executes an WhoAmI request aginst Dataverse and returns the result as a JSON string.")]
public static string WhoAmI(IOrganizationService orgService)
{
try
{
WhoAmIRequest req = new WhoAmIRequest();
var whoAmIResult = orgService.Execute(req);
return Newtonsoft.Json.JsonConvert.SerializeObject(whoAmIResult);
}
catch (Exception err)
{
Console.Error.WriteLine(err.ToString());
return err.ToString();
}
}
[McpServerTool, Description("Executes an FetchXML request using the supplied expression that needs to be a valid FetchXml expression. Returns the result as a JSON string. If the request fails, the response will be prepended with [ERROR] and the error should be presented to the user.")]
public static string ExecuteFetch(string fetchXmlRequest, IOrganizationService orgService)
{
try
{
FetchExpression fetchExpression = new FetchExpression(fetchXmlRequest);
EntityCollection result = orgService.RetrieveMultiple(fetchExpression);
return Newtonsoft.Json.JsonConvert.SerializeObject(result);
}
catch (Exception err)
{
var errorString = "[ERROR] " + err.ToString();
Console.Error.WriteLine(err.ToString());
return errorString;
}
}
}