Skip to content

Commit 6f09e74

Browse files
committed
Refactor NetContextServer and NetContextClient to use empty array syntax for parameters
- Update calls to `CallToolAsync` in Program.cs, FileService.cs, and various test files to use empty array syntax instead of `new Dictionary<string, object>()`. - Refactor JSON deserialization options to utilize a consistent `DefaultJsonOptions` across tests. - Improve error handling in FileTools.cs by using a predefined error message array.
1 parent cc53854 commit 6f09e74

12 files changed

Lines changed: 73 additions & 112 deletions

src/NetContextClient/Program.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ static async Task<int> Main(string[] args)
4141
{
4242
try
4343
{
44-
var result = await client.CallToolAsync("hello", new Dictionary<string, object>());
44+
var result = await client.CallToolAsync("hello", []);
4545
var content = result.Content.First(c => c.Type == "text");
4646
await Console.Out.WriteLineAsync(content.Text);
4747
}
@@ -82,7 +82,7 @@ static async Task<int> Main(string[] args)
8282
{
8383
try
8484
{
85-
var result = await client.CallToolAsync("get_base_directory", new Dictionary<string, object>());
85+
var result = await client.CallToolAsync("get_base_directory", []);
8686
var jsonResponse = result.Content.First(c => c.Type == "text").Text;
8787

8888
try
@@ -140,7 +140,7 @@ static async Task<int> Main(string[] args)
140140
{
141141
try
142142
{
143-
var result = await client.CallToolAsync("list_projects", new Dictionary<string, object>());
143+
var result = await client.CallToolAsync("list_projects", []);
144144
var jsonText = result.Content.First(c => c.Type == "text").Text;
145145
if (jsonText != null)
146146
{
@@ -234,7 +234,7 @@ static async Task<int> Main(string[] args)
234234
{
235235
try
236236
{
237-
var result = await client.CallToolAsync("list_solutions", new Dictionary<string, object>());
237+
var result = await client.CallToolAsync("list_solutions", []);
238238
var jsonText = result.Content.First(c => c.Type == "text").Text;
239239
if (jsonText != null)
240240
{
@@ -416,7 +416,7 @@ static async Task<int> Main(string[] args)
416416
{
417417
try
418418
{
419-
var result = await client.CallToolAsync("get_state_file_location", new Dictionary<string, object>());
419+
var result = await client.CallToolAsync("get_state_file_location", []);
420420
var jsonText = result.Content.First(c => c.Type == "text").Text;
421421
if (jsonText != null)
422422
{
@@ -437,7 +437,7 @@ static async Task<int> Main(string[] args)
437437
{
438438
try
439439
{
440-
var result = await client.CallToolAsync("get_ignore_patterns", new Dictionary<string, object>());
440+
var result = await client.CallToolAsync("get_ignore_patterns", []);
441441
var jsonText = result.Content.First(c => c.Type == "text").Text;
442442
if (jsonText != null)
443443
{
@@ -472,7 +472,7 @@ static async Task<int> Main(string[] args)
472472
{
473473
try
474474
{
475-
var result = await client.CallToolAsync("clear_ignore_patterns", new Dictionary<string, object>());
475+
var result = await client.CallToolAsync("clear_ignore_patterns", []);
476476
var jsonText = result.Content.First(c => c.Type == "text").Text;
477477
if (jsonText != null)
478478
{
@@ -544,7 +544,7 @@ static async Task<int> Main(string[] args)
544544
{
545545
try
546546
{
547-
var result = await client.CallToolAsync("analyze_packages", new Dictionary<string, object>());
547+
var result = await client.CallToolAsync("analyze_packages", []);
548548
var jsonText = result.Content.First(c => c.Type == "text").Text;
549549

550550
// Try to deserialize to our expected type

src/NetContextServer/Services/FileService.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ namespace NetContextServer.Services;
33
internal static class FileService
44
{
55
public static readonly string[] DotNetFilePatterns =
6-
{
6+
[
77
"*.cs", // C# source files
88
"*.vb", // Visual Basic source files
99
"*.fs", // F# source files
@@ -12,7 +12,7 @@ internal static class FileService
1212
"*.cshtml", // Razor views
1313
"*.vbhtml", // VB Razor views
1414
"*.razor" // Blazor components
15-
};
15+
];
1616

1717
public static string[] ListProjects()
1818
{
@@ -71,7 +71,7 @@ public static string[] ListSourceFiles(string projectDir)
7171
.Where(f => !IgnorePatternService.ShouldIgnoreFile(f)));
7272
}
7373

74-
return allFiles.ToArray();
74+
return [.. allFiles];
7575
}
7676

7777
public static string OpenFile(string filePath)

src/NetContextServer/Services/PackageAnalyzerService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public async Task<PackageAnalysis> AnalyzePackageAsync(PackageReference package)
8383
try
8484
{
8585
var content = await File.ReadAllTextAsync(file);
86-
var lines = content.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
86+
var lines = content.Split(["\r\n", "\r", "\n"], StringSplitOptions.None);
8787

8888
// Enhanced usage detection with proper line splitting
8989
foreach (var line in lines)

src/NetContextServer/Services/SemanticSearchService.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ internal class SemanticSearchService
2323
"**/*.g.cs",
2424
"**/*.AssemblyInfo.cs"
2525
];
26-
private bool _credentialsAvailable = false;
26+
private readonly bool _credentialsAvailable = false;
27+
28+
private static readonly JsonSerializerOptions DefaultJsonOptions = new()
29+
{
30+
PropertyNameCaseInsensitive = true
31+
};
2732

2833
public SemanticSearchService()
2934
{
@@ -159,14 +164,10 @@ private static bool ShouldIgnoreFile(string filePath)
159164
{
160165
// Get user patterns from IgnorePatternService
161166
var userPatternsJson = IgnorePatternService.GetIgnorePatterns();
162-
var options = new JsonSerializerOptions
163-
{
164-
PropertyNameCaseInsensitive = true
165-
};
166167

167168
try
168169
{
169-
var response = JsonSerializer.Deserialize<IgnorePatternsResponse>(userPatternsJson, options);
170+
var response = JsonSerializer.Deserialize<IgnorePatternsResponse>(userPatternsJson, DefaultJsonOptions);
170171
var allPatterns = _defaultIgnorePatterns.Concat(response?.UserPatterns ?? []);
171172

172173
foreach (var pattern in allPatterns)

src/NetContextServer/Tools/FileTools.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using ModelContextProtocol;
21
using ModelContextProtocol.Server;
3-
using NetContextServer.Models;
42
using NetContextServer.Services;
53
using System.ComponentModel;
64
using System.Text.Json;
@@ -16,6 +14,8 @@ public static class FileTools
1614
WriteIndented = true
1715
};
1816

17+
private static readonly string[] ErrorDirectoryNotFound = ["Error: Directory not found"];
18+
1919
[McpTool("list_files")]
2020
[Description("Lists all .NET source files in the specified project directory.")]
2121
public static string ListFiles(
@@ -62,7 +62,7 @@ public static string SetBaseDirectory(
6262
{
6363
if (!Directory.Exists(directory))
6464
{
65-
return JsonSerializer.Serialize(new[] { "Error: Directory not found" }, DefaultJsonOptions);
65+
return JsonSerializer.Serialize(ErrorDirectoryNotFound, DefaultJsonOptions);
6666
}
6767

6868
FileValidationService.SetBaseDirectory(directory);

tests/NetContextServer/BaseDirectoryTests.cs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
using ModelContextProtocol.Client;
22
using System.Text.Json;
3-
using Xunit;
43

54
namespace NetContextServer.Tests;
65

76
[Collection("NetContextServer Collection")]
8-
public class BaseDirectoryTests : IAsyncLifetime
7+
public class BaseDirectoryTests(NetContextServerFixture fixture) : IAsyncLifetime
98
{
10-
private readonly string _testDir;
11-
private readonly IMcpClient _client;
9+
private readonly string _testDir = Path.Combine(Path.GetTempPath(), "NetContextServerBaseDirectoryTests_" + Guid.NewGuid());
10+
private readonly IMcpClient _client = fixture.Client;
1211

13-
public BaseDirectoryTests(NetContextServerFixture fixture)
12+
private static readonly JsonSerializerOptions DefaultJsonOptions = new()
1413
{
15-
_client = fixture.Client;
16-
_testDir = Path.Combine(Path.GetTempPath(), "NetContextServerBaseDirectoryTests_" + Guid.NewGuid());
17-
}
14+
PropertyNameCaseInsensitive = true
15+
};
1816

1917
public async Task InitializeAsync()
2018
{
@@ -105,7 +103,7 @@ await _client.CallToolAsync("set_base_directory",
105103

106104
// Act
107105
var result = await _client.CallToolAsync("get_base_directory",
108-
new Dictionary<string, object>());
106+
[]);
109107

110108
// Assert
111109
Assert.NotNull(result);
@@ -115,7 +113,7 @@ await _client.CallToolAsync("set_base_directory",
115113

116114
var response = JsonSerializer.Deserialize<BaseDirectoryResponse>(
117115
content.Text,
118-
new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
116+
DefaultJsonOptions);
119117

120118
Assert.NotNull(response);
121119
Assert.Equal(tempDir, response.BaseDirectory);
@@ -138,7 +136,7 @@ await _client.CallToolAsync("set_base_directory",
138136

139137
// Act
140138
var result = await _client.CallToolAsync("get_base_directory",
141-
new Dictionary<string, object>());
139+
[]);
142140

143141
// Assert
144142
Assert.NotNull(result);
@@ -148,7 +146,7 @@ await _client.CallToolAsync("set_base_directory",
148146

149147
var response = JsonSerializer.Deserialize<BaseDirectoryResponse>(
150148
content.Text,
151-
new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
149+
DefaultJsonOptions);
152150

153151
Assert.NotNull(response);
154152
Assert.Equal(tempDir, response.BaseDirectory);

tests/NetContextServer/BasicServerTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public async Task ListTools_ReturnsValidToolList()
5050
public async Task Hello_ReturnsSuccess()
5151
{
5252
// Act
53-
var result = await _client.CallToolAsync("hello", new Dictionary<string, object>());
53+
var result = await _client.CallToolAsync("hello", []);
5454

5555
// Assert
5656
Assert.NotNull(result);
@@ -66,15 +66,15 @@ public async Task CallInvalidTool_ReturnsError()
6666
{
6767
// Act & Assert
6868
var ex = await Assert.ThrowsAsync<McpClientException>(
69-
() => _client.CallToolAsync("invalid_tool", new Dictionary<string, object>()));
69+
() => _client.CallToolAsync("invalid_tool", []));
7070
Assert.Contains("Unknown tool", ex.Message);
7171
}
7272

7373
[Fact]
7474
public async Task CallToolWithInvalidParameters_ReturnsError()
7575
{
7676
// Act
77-
var result = await _client.CallToolAsync("list_files", new Dictionary<string, object>());
77+
var result = await _client.CallToolAsync("list_files", []);
7878

7979
// Assert
8080
Assert.NotNull(result);

tests/NetContextServer/IgnoreOperationTests.cs

Lines changed: 23 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,38 @@
11
using ModelContextProtocol.Client;
22
using System.Text.Json;
3-
using Xunit;
43

54
namespace NetContextServer.Tests;
65

76
[Collection("NetContextServer Collection")]
8-
public class IgnoreOperationTests : IAsyncLifetime
7+
public class IgnoreOperationTests(NetContextServerFixture fixture) : IAsyncLifetime
98
{
10-
private readonly IMcpClient _client;
11-
private readonly string _testDir;
9+
private readonly IMcpClient _client = fixture.Client;
10+
private readonly string _testDir = Path.Combine(Path.GetTempPath(), "NetContextServerTests_" + Guid.NewGuid());
11+
12+
private static readonly JsonSerializerOptions DefaultJsonOptions = new()
13+
{
14+
PropertyNameCaseInsensitive = true,
15+
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
16+
};
1217

1318
private class AddIgnorePatternsResponse
1419
{
15-
public string[] InvalidPatterns { get; set; } = Array.Empty<string>();
16-
public string[] ValidPatternsAdded { get; set; } = Array.Empty<string>();
17-
public string[] AllPatterns { get; set; } = Array.Empty<string>();
20+
public string[] InvalidPatterns { get; set; } = [];
21+
public string[] ValidPatternsAdded { get; set; } = [];
22+
public string[] AllPatterns { get; set; } = [];
1823
}
1924

2025
private class IgnorePatternsResponse
2126
{
22-
public string[] DefaultPatterns { get; set; } = Array.Empty<string>();
23-
public string[] UserPatterns { get; set; } = Array.Empty<string>();
27+
public string[] DefaultPatterns { get; set; } = [];
28+
public string[] UserPatterns { get; set; } = [];
2429
}
2530

2631
private class ErrorResponse
2732
{
2833
public string Error { get; set; } = string.Empty;
2934
}
3035

31-
public IgnoreOperationTests(NetContextServerFixture fixture)
32-
{
33-
_client = fixture.Client;
34-
_testDir = Path.Combine(Path.GetTempPath(), "NetContextServerTests_" + Guid.NewGuid());
35-
}
36-
3736
public async Task InitializeAsync()
3837
{
3938
Directory.CreateDirectory(_testDir);
@@ -85,14 +84,8 @@ public async Task AddIgnorePatterns_AddsNewPatterns()
8584

8685
// Debug output
8786
Console.WriteLine($"Actual JSON response: {content.Text}");
88-
89-
var options = new JsonSerializerOptions
90-
{
91-
PropertyNameCaseInsensitive = true,
92-
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
93-
};
9487

95-
var response = JsonSerializer.Deserialize<AddIgnorePatternsResponse>(content.Text, options);
88+
var response = JsonSerializer.Deserialize<AddIgnorePatternsResponse>(content.Text, DefaultJsonOptions);
9689
Assert.NotNull(response);
9790
Assert.Empty(response.InvalidPatterns);
9891
Assert.Equal(patterns.Length, response.ValidPatternsAdded.Length);
@@ -110,21 +103,15 @@ await _client.CallToolAsync("add_ignore_patterns",
110103

111104
// Act
112105
var result = await _client.CallToolAsync("clear_ignore_patterns",
113-
new Dictionary<string, object>());
106+
[]);
114107

115108
// Assert
116109
Assert.NotNull(result);
117110
var content = result.Content.FirstOrDefault(c => c.Type == "text");
118111
Assert.NotNull(content);
119112
Assert.NotNull(content.Text);
120-
121-
var options = new JsonSerializerOptions
122-
{
123-
PropertyNameCaseInsensitive = true,
124-
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
125-
};
126113

127-
var response = JsonSerializer.Deserialize<IgnorePatternsResponse>(content.Text, options);
114+
var response = JsonSerializer.Deserialize<IgnorePatternsResponse>(content.Text, DefaultJsonOptions);
128115
Assert.NotNull(response);
129116
Assert.NotEmpty(response.DefaultPatterns);
130117
Assert.Empty(response.UserPatterns);
@@ -140,21 +127,15 @@ await _client.CallToolAsync("add_ignore_patterns",
140127

141128
// Act
142129
var result = await _client.CallToolAsync("get_ignore_patterns",
143-
new Dictionary<string, object>());
130+
[]);
144131

145132
// Assert
146133
Assert.NotNull(result);
147134
var content = result.Content.FirstOrDefault(c => c.Type == "text");
148135
Assert.NotNull(content);
149136
Assert.NotNull(content.Text);
150-
151-
var options = new JsonSerializerOptions
152-
{
153-
PropertyNameCaseInsensitive = true,
154-
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
155-
};
156137

157-
var response = JsonSerializer.Deserialize<IgnorePatternsResponse>(content.Text, options);
138+
var response = JsonSerializer.Deserialize<IgnorePatternsResponse>(content.Text, DefaultJsonOptions);
158139
Assert.NotNull(response);
159140
Assert.NotEmpty(response.DefaultPatterns);
160141
Assert.Equal(2, response.UserPatterns.Length);
@@ -191,9 +172,9 @@ await _client.CallToolAsync("add_ignore_patterns",
191172

192173
private class RemoveIgnorePatternsResponse
193174
{
194-
public string[] RemovedPatterns { get; set; } = Array.Empty<string>();
195-
public string[] NotFoundPatterns { get; set; } = Array.Empty<string>();
196-
public string[] DefaultPatternsSkipped { get; set; } = Array.Empty<string>();
197-
public string[] AllPatterns { get; set; } = Array.Empty<string>();
175+
public string[] RemovedPatterns { get; set; } = [];
176+
public string[] NotFoundPatterns { get; set; } = [];
177+
public string[] DefaultPatternsSkipped { get; set; } = [];
178+
public string[] AllPatterns { get; set; } = [];
198179
}
199180
}

tests/NetContextServer/NetContextServerFixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public async Task InitializeAsync()
118118
// Verify connection with a hello request
119119
using var helloCts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
120120
var helloResult = await Client.CallToolAsync("hello",
121-
new Dictionary<string, object>(), helloCts.Token);
121+
[], helloCts.Token);
122122

123123
if (helloResult.IsError)
124124
{

0 commit comments

Comments
 (0)