Skip to content

Object properties are returning null value that should not do so #527

Closed
@MovGP0

Description

@MovGP0

Describe the bug
Currenty many properties are null by default that should have a default value that is not null.

The best practice is that properties shold return default values/objects that can be used instead of returning null values. The user should still be able to provide custom implementations of the objects if needed.

To Reproduce
Example when setting up McpServerOptions. The following code throws an Object reference not set to an instance of an object exception:

	McpServerOptions options = new()
	{
		ServerInfo =
		{
			Name = "MyServer",
			Version = "1.0.0"
		},
		Capabilities = {
			Tools = {
                            // ...
			}
		}
	};

Problem description

This behavior forces users to create new instances of the objects:

	McpServerOptions options = new()
	{
		ServerInfo = new()
		{
			Name = "MyServer",
			Version = "1.0.0"
		},
		Capabilities = new() {
			Tools = new() {
                            ListChanged = true,
                            ToolCollection = new() {
                                // ...
                             }
			}
		}
	};

Also, every time the user want to access the properties, a lot of null checks are required:

// current behavior
var handler = options.Capabilities?.Tools?.ListToolsHandler;

// expected behavior
var handler = options.Capabilities.Tools.ListToolsHandler;

Expected behavior

  • Properties should provide default objects that can readily be used.
  • Users should be able to set the values of the properties to other object instances

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions