from mcp.types import ToolAnnotations
from mcp.server.fastmcp import FastMCP
import sys

server=FastMCP("Sum MCP Server")
print(server,file=sys.stderr)

@server.tool()
def sum_array(arr:list) -> dict:
    '''Every object of arr must be an array of 2 objects: the first must be a string, the second must be an integer. I sum all the integers that the corresponding string appears only once in arr. I return a dict containing the keys sum and excluded. excluded contains a list of the strings wich their arrays were excluded of the sum.'''
    ret={"sum":0, "excluded":None}
    included=set()
    excluded=set()
    conta=[]
    soma=0
    for a in arr:
        name=a[0]
        if name in included:
            if name not in excluded:
                for t in conta:
                    if t[0]==name:
                        soma-=t[1]
                        break
                excluded.add(name)
        else:
            included.add(name)
            soma+=a[1]
            conta.append((a[0],a[1]))
    ret["sum"]=soma
    ret["excluded"]=list(excluded)
    return ret

if __name__=="__main__":
    server.run(transport="stdio")
    print("server: ending execution.",file=sys.stderr)
