Skip to content

Commit 0961b1d

Browse files
Add some resources to the server
1 parent df1d052 commit 0961b1d

File tree

4 files changed

+615
-0
lines changed

4 files changed

+615
-0
lines changed

mcp_nexus/Models/McpModels.cs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,53 @@ public class McpToolsListResult
110110
public McpToolSchema[] Tools { get; set; } = Array.Empty<McpToolSchema>();
111111
}
112112

113+
// ===== MCP RESOURCE MODELS =====
114+
115+
public class McpResource
116+
{
117+
[JsonPropertyName("uri")]
118+
public string Uri { get; set; } = string.Empty;
119+
120+
[JsonPropertyName("name")]
121+
public string Name { get; set; } = string.Empty;
122+
123+
[JsonPropertyName("description")]
124+
public string Description { get; set; } = string.Empty;
125+
126+
[JsonPropertyName("mimeType")]
127+
public string? MimeType { get; set; }
128+
129+
[JsonPropertyName("expiresAt")]
130+
public DateTime? ExpiresAt { get; set; }
131+
}
132+
133+
public class McpResourcesListResult
134+
{
135+
[JsonPropertyName("resources")]
136+
public McpResource[] Resources { get; set; } = Array.Empty<McpResource>();
137+
}
138+
139+
public class McpResourceContent
140+
{
141+
[JsonPropertyName("uri")]
142+
public string Uri { get; set; } = string.Empty;
143+
144+
[JsonPropertyName("mimeType")]
145+
public string MimeType { get; set; } = "text/plain";
146+
147+
[JsonPropertyName("text")]
148+
public string? Text { get; set; }
149+
150+
[JsonPropertyName("blob")]
151+
public string? Blob { get; set; }
152+
}
153+
154+
public class McpResourceReadResult
155+
{
156+
[JsonPropertyName("contents")]
157+
public McpResourceContent[] Contents { get; set; } = Array.Empty<McpResourceContent>();
158+
}
159+
113160
public class McpServerInfoResponse
114161
{
115162
[JsonPropertyName("jsonrpc")]
@@ -132,11 +179,20 @@ public class McpCapabilities
132179
{
133180
[JsonPropertyName("tools")]
134181
public object Tools { get; set; } = new { listChanged = true };
182+
183+
[JsonPropertyName("resources")]
184+
public object Resources { get; set; } = new
185+
{
186+
subscribe = true,
187+
listChanged = true
188+
};
189+
135190
[JsonPropertyName("notifications")]
136191
public object Notifications { get; set; } = new
137192
{
138193
// Standard MCP notifications
139194
tools = new { listChanged = true },
195+
resources = new { listChanged = true },
140196
// Custom MCP Nexus notifications
141197
commandStatus = true,
142198
sessionRecovery = true,

mcp_nexus/Program.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,10 @@ private static void RegisterServices(IServiceCollection services, IConfiguration
697697
// Register MCP notification service for both HTTP and stdio modes
698698
services.AddSingleton<IMcpNotificationService, McpNotificationService>();
699699
Console.Error.WriteLine("Registered McpNotificationService for server-initiated notifications");
700+
701+
// Register MCP resource service for context and data resources
702+
services.AddSingleton<McpResourceService>();
703+
Console.Error.WriteLine("Registered McpResourceService for MCP resources support");
700704
}
701705

702706
private static void ConfigureHttpServices(IServiceCollection services)

mcp_nexus/Protocol/McpProtocolService.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace mcp_nexus.Protocol
88
public class McpProtocolService(
99
McpToolDefinitionService m_toolDefinitionService,
1010
McpToolExecutionService m_toolExecutionService,
11+
McpResourceService m_resourceService,
1112
ILogger<McpProtocolService> m_logger)
1213
{
1314
public async Task<object> ProcessRequest(JsonElement requestElement)
@@ -75,6 +76,8 @@ private async Task<object> ExecuteMethod(McpRequest request)
7576
"notifications/initialized" => HandleNotificationInitialized(),
7677
"tools/list" => HandleToolsList(),
7778
"tools/call" => await HandleToolsCall(request.Params),
79+
"resources/list" => HandleResourcesList(),
80+
"resources/read" => await HandleResourcesRead(request.Params),
7881
"notifications/cancelled" => HandleNotificationCancelled(request.Params),
7982
_ => throw new McpToolException(-32601, $"Method not found: {request.Method}")
8083
};
@@ -125,6 +128,28 @@ private object HandleToolsList()
125128
return new McpToolsListResult { Tools = tools };
126129
}
127130

131+
private object HandleResourcesList()
132+
{
133+
var resources = m_resourceService.GetAllResources();
134+
return new McpResourcesListResult { Resources = resources };
135+
}
136+
137+
private async Task<object> HandleResourcesRead(JsonElement? paramsElement)
138+
{
139+
if (paramsElement == null || !paramsElement.Value.TryGetProperty("uri", out var uriProperty))
140+
{
141+
throw new McpToolException(-32602, "Missing required parameter: uri");
142+
}
143+
144+
var uri = uriProperty.GetString();
145+
if (string.IsNullOrEmpty(uri))
146+
{
147+
throw new McpToolException(-32602, "Invalid uri parameter");
148+
}
149+
150+
return await m_resourceService.ReadResource(uri);
151+
}
152+
128153
private async Task<object> HandleToolsCall(JsonElement? paramsElement)
129154
{
130155
if (paramsElement == null)

0 commit comments

Comments
 (0)